為了更好地管理Android應用的用戶界面里的各組件,Android提供了佈局管理器。通過使用佈局管理器,Android應用圖形用戶界面具有良好的平臺無關性。推薦使用佈局管理器來管理組件的分佈、大小,而不是直接設置組件的位置和大小。可以使用佈局管理器嵌套佈局管理器,即也可作為一個UI組件來使用。 L ...
為了更好地管理Android應用的用戶界面里的各組件,Android提供了佈局管理器。通過使用佈局管理器,Android應用圖形用戶界面具有良好的平臺無關性。推薦使用佈局管理器來管理組件的分佈、大小,而不是直接設置組件的位置和大小。可以使用佈局管理器嵌套佈局管理器,即也可作為一個UI組件來使用。
LinearLayout可以控制組件橫向排列或者縱向排列,內容不會換行,超出屏幕部分將不會顯示出來。
學習圖解
LinearLayout 常用XML屬性及方法
【屬性一】orientation 設置子組件的排列方式(單選)
XML: android:orientation="horizontal"
horizontal:橫向排列
vertical:縱向排列
JAVA :linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.HORIZONTAL 橫向排列
LinearLayout.VERTICAL 縱向排列
【屬性二】gravity 設置子組件的對齊方式(多選)
XML: android:gravity="center"
JAVA :linearLayout.setGravity(Gravity.CENTER);
【屬性三】baselineAligned 設置子元素基準線對棄,預設為true
基準線:
打開的英語練習本,那條紅線就是基準線
XML: android:baselineAligned="false"
JAVA: linearLayout.setBaselineAligned(true);
代碼:true
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:padding="20dp"
android:text="text1"
android:textSize="30sp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:padding="10dp"
android:text="text2"
android:textSize="16sp">
</TextView>
</LinearLayout>
效果:
【搭配屬性三】baselineAlignedChildIndex LinearLayout的基準線以他的第幾個子元素為準,下標從0開始
一個LinearLayout 裡面有很多 textview ,每一個 textview 都有自己的基準線,那麼LinearLayout可能也是另一個LinearLayout的子元素,作為子元素 baselineAlignedChildIndex 就決定這他的一個基準線
XML:android:baselineAlignedChildIndex="0"
JAVA:linearLayout.setBaselineAlignedChildIndex(0);
代碼:⭐註意內部的LinearLayout,後面將在 第二個LinearLayout上添加 baselineAlignedChildIndex ,搭配 baselineAligned="false" 使用
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_light"
android:text="這是text2"
android:textSize="20sp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:text="這是text1"
android:textSize="30sp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
android:text="這是text2"
android:textSize="15sp">
</TextView>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是text4"
android:textSize="25sp"
android:background="@android:color/holo_orange_light"
>
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:text="text"
android:textColor="@android:color/white"
android:textSize="15sp">
</TextView>
</LinearLayout>
效果:
![](https://img2020.cnblogs.com/blog/1376767/202003/1376767-20200331102545109-1792496510.png)
⭐ 總結
- 預設LinearLayout是沒有基準線的,從圖一和圖三的對比可知。
- 下標從0開始三個子組件,最大index為2,超過2時佈局將不顯示
- 這個屬性是用來決定當前LinearLayout的基準線時以哪個子組件為準的