Android超詳細(xì)講解組件LinearLayout的使用
概述
LinearLayout是線性布局組件,放置在其中的組件按列或者按行(就是垂直或者水平)的方式排序分布。
常用XML配置屬性
(1) android:orientation
設(shè)置LinearLayout容器布局組件的方式:只能取值:horizontal(水平的),vertical(垂直的)
(2) android:gravity
設(shè)置布局在LinearLayout容器內(nèi)的組件對(duì)齊方式。
取值包括top, bottom, left, right, center, start, end(上,下,左,右,中,開始,結(jié)束)
(3) View中繼承來的屬性
(包括android:background ,android:visibility等。還有一些改善美觀的放置組件的間隔)
1. android:layout_width和android:layout_height (match_parent/wrap_content)
2 .android:layout_gravity 設(shè)置組件在容器中的布局
3. android:layout_weight 設(shè)置組件占用空間的空余顯示空間的比列
4. android:layout_margin ,android:layout_marginTop ,android:layout_marginBottom ,android:layout_marginLeft ,android:layout_marginRight 設(shè)置組件的外邊界,類似我們搞網(wǎng)頁設(shè)計(jì)HTML/CSS中margin用法。
代碼舉例
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RadioGroup android:id="@+id/orientation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" > <RadioButton android:id="@+id/horizontal" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="水平" android:textSize="30dp" /> <RadioButton android:id="@+id/vertical" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="垂直" android:textSize="30dp" /> </RadioGroup> <RadioGroup android:id="@+id/gravity" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" > <RadioButton android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="居左" android:textSize="30dp" /> <RadioButton android:id="@+id/center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="居中" android:textSize="30dp" /> <RadioButton android:id="@+id/right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="居右" android:textSize="30dp" /> </RadioGroup> </LinearLayout>
MainActivity.java
package com.example.android_demo02; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.widget.LinearLayout; import android.widget.RadioGroup; public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { private RadioGroup orientation; private RadioGroup gravity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); orientation=(RadioGroup) findViewById(R.id.orientation); orientation.setOnCheckedChangeListener(this); gravity=(RadioGroup) findViewById(R.id.gravity); gravity.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i){ case R.id.horizontal: orientation.setOrientation(LinearLayout.HORIZONTAL); break; case R.id.vertical: orientation.setOrientation(LinearLayout.VERTICAL); break; case R.id.left: gravity.setGravity(Gravity.START); break; case R.id.center: gravity.setGravity(Gravity.CENTER_HORIZONTAL); break; case R.id.right: gravity.setGravity(Gravity.END); break; } } }
實(shí)現(xiàn)效果:
到此這篇關(guān)于Android超詳細(xì)講解組件LinearLayout的使用的文章就介紹到這了,更多相關(guān)Android LinearLayout內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android LinearLayout實(shí)現(xiàn)自動(dòng)換行效果
- Android LinearLayout實(shí)現(xiàn)自動(dòng)換行
- Android自定義LinearLayout布局顯示不完整的解決方法
- Android使用LinearLayout設(shè)置邊框
- Android編程使用LinearLayout和PullRefreshView實(shí)現(xiàn)上下翻頁功能的方法
- android中LinearLayoutManager一鍵返回頂部示例
- Android中的LinearLayout布局
- Android基礎(chǔ)之獲取LinearLayout的寬高
相關(guān)文章
Android自定義View實(shí)現(xiàn)比賽時(shí)間閃動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)比賽時(shí)間閃動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Android Studio gradle 編譯提示‘default not found’ 解決辦法
這篇文章主要介紹了Android Studio gradle 編譯提示‘default not found’ 解決辦法的相關(guān)資料,需要的朋友可以參考下2016-12-12Android編程實(shí)現(xiàn)WebView自適應(yīng)全屏方法小結(jié)
這篇文章主要介紹了Android編程實(shí)現(xiàn)WebView自適應(yīng)全屏方法,結(jié)合實(shí)例形式總結(jié)了三種常用的WebView自適應(yīng)全屏實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12Android自定義View實(shí)現(xiàn)縱向跑馬燈效果詳解
對(duì)于跑馬燈效果在我們?nèi)粘J褂玫腶pp中還是很常見的,比如外賣app的商家公告就使用了此效果,但是它是橫向滾動(dòng)的,橫向滾動(dòng)多適用于單條信息;但凡涉及到多條信息的滾動(dòng)展示,用縱向滾動(dòng)效果會(huì)有更好的用戶體驗(yàn),今天我們通過自定義View來看看如何實(shí)現(xiàn)縱向跑馬燈效果。2016-11-11Android實(shí)用控件自定義逼真相機(jī)光圈View
這篇文章主要為大家詳細(xì)介紹了Android實(shí)用控件自定義逼真相機(jī)光圈,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)
這篇文章主要為大家介紹了Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android自定義控件實(shí)現(xiàn)優(yōu)雅的廣告輪播圖
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)優(yōu)雅的廣告輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03android開發(fā)實(shí)踐之ndk編譯命令簡單示例
這篇文章主要給大家介紹了在android中ndk編譯命令使用的相關(guān)資料,文中詳細(xì)介紹了ndk-build命令行參數(shù),并通過簡單的示例代碼給大家介紹了如何編寫 .c 文件,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06