Android中LinearLayout布局的常用屬性總結(jié)
基本屬性要求
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout>
- android:orientation
- 決定是水平排列或是垂直排列
- vertical 垂直排列
- horizontal 水平排列
垂直排列 Button
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" /> </LinearLayout>
水平排列 Button
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" /> </LinearLayout>
重心設(shè)定
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="left"> </LinearLayout>
- android:gravity
- 設(shè)定框架的內(nèi)容的放置方向
- center 水平垂直皆置中
- center_vertical 垂直置中
- center_horizontal 水平置中
- top 置頂
- left 置左
- bottom 置底
- right 置右
水平、垂直置中
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout>
透過 OR 運(yùn)算子組合重心
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="top|right"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="bottom|left"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical|center_horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout>
比例分配
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_weight="1"/> </LinearLayout>
- android:layout_weight
- 子元件或子框架的比重。
- LinearLayout 下的子元件或子框架,才能設(shè)定這項(xiàng)屬性。
等比例分配
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_weight="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" android:layout_weight="1"/> </LinearLayout>
比重都是 1,所以大小相同。
非等比例分配
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_weight=".10"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_weight=".20"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" android:layout_weight=".70"/> </LinearLayout>
.10 代表 0.10
.20 代表 0.20
.70 代表 0.70
合起來剛好是 1 ,作 100% 分配。
相關(guān)文章
Android UI實(shí)現(xiàn)單行文本水平觸摸滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android UI實(shí)現(xiàn)單行文本水平觸摸滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android使用Notification實(shí)現(xiàn)通知功能
這篇文章主要為大家詳細(xì)介紹了Android使用Notification實(shí)現(xiàn)通知功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Flutter持久化存儲(chǔ)之?dāng)?shù)據(jù)庫存儲(chǔ)(sqflite)詳解
這篇文章主要給大家介紹了關(guān)于Flutter持久化存儲(chǔ)之?dāng)?shù)據(jù)庫存儲(chǔ)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03基于Android實(shí)現(xiàn)數(shù)獨(dú)游戲
這篇文章主要為大家詳細(xì)介紹了基于Android實(shí)現(xiàn)數(shù)獨(dú)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android時(shí)間日期拾取器學(xué)習(xí)使用(DatePicker、TimePicker)
這篇文章主要為大家詳細(xì)介紹了Android提供的DatePicker日期拾取器和TimePicker時(shí)間拾取器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02