Android開(kāi)發(fā)學(xué)習(xí)教程(12)- Android布局之線性布局LinearLayout
—— 當(dāng)下的生活或許疲憊又難熬,但你要相信,始終沒(méi)有放棄過(guò)的你,一定會(huì)過(guò)上想要的生活。
上一篇我們講了對(duì)話框AlertDialog的基本用法,這里來(lái)學(xué)習(xí)常用布局之線性布局的基本用法。
線性布局是什么
線性布局中的控件按照橫向或豎向排列,并且線性布局不會(huì)換行,當(dāng)控件超出屏幕邊緣,后面的控件就被隱藏,不會(huì)被顯示出來(lái)。
線性布局有什么用
控制其中的控件只能橫向或豎向排列。
線性布局怎么用
繼續(xù)基于上一篇的項(xiàng)目,我們新建一個(gè)LinearLayoutActivity:
<?
xml
?version
=
"1.0"
?encoding
=
"utf-8"
?>
<
LinearLayout
?xmlns:android
=
"http://schemas.android.com/apk/res/android"
????
android:layout_width
=
"match_parent"
????
android:layout_height
=
"match_parent"
????
android:gravity
=
"center"
????
android:orientation
=
"horizontal"
>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:text
=
"Hello 我是第一個(gè)子控件"
?/>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:text
=
"Hello 我是第二個(gè)子控件"
?/>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:text
=
"Hello 我是第三個(gè)子控件"
?/>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:text
=
"Hello 我是第四個(gè)子控件"
?/>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:text
=
"Hello 我是第五個(gè)子控件"
?/>
</
LinearLayout
>
上圖橫向排列了五個(gè)控件,并且線性布局不會(huì)自動(dòng)換行,當(dāng)控件超出屏幕邊緣,后面的控件就被隱藏,不會(huì)被顯示出來(lái)。
LinearLayout基本屬性
android:gravity:用來(lái)控制子控件的位置,值有top,left,right,bottom,center,分別表示子控件在頂部、左部、右部、下部、垂直方向居中并且水平方向居中,值得注意的是,還可以組合取值,如right|center_vertical表示子控件在右部并且垂直方向居中;
android:orientation:用來(lái)控制子控件的排列方式,值為horizontal時(shí)表示所有子控件橫向排列,為vertical時(shí)表示所有子控件豎向排列,上面的例子是橫向排列,我們來(lái)試試豎向排列;
把a(bǔ)ndroid:orientation改為vertical,為了使效果更明顯,我們?nèi)サ鬭ndroid:gravity設(shè)置,使用默認(rèn)值(top|left),運(yùn)行如下:
源碼鏈接:https://yunjunet.cn/876750.html