LinearLayout是android studio中最基本的布局,它通过指定android:orientation属性以水平或垂直方式顺序对齐所有子级。如果应用android:orientation =“ vertical”,则元素将以垂直方式一个接一个地排列;如果应用android:orientation =“ horizontal”,则元素将以水平方式一个接一个地排列。
LinearLayout的示例代码
XML
XML
XML
XML
XML
XML
LinearLayout的一些重要属性
Attributes |
Description |
---|---|
android:layout_weight |
It is defined individually to the child’s views to specify how LinearLayout divides the remaining space amongst the views it contains |
android:weightSum | Defines the total weight sum |
android:orientation | How the elements should be arranged in the layout. It can be horizontal or vertical. |
android:gravity |
It specifies how an object should position its content on its X and Y axes. Possible values are – center_vertical, fill, center, bottom, end, etc. |
android:layout_gravity |
Sets the gravity of the View or Layout relative to its parent. Possible values are – center_vertical, fill, center, bottom, end, etc. |
android:baselineAligned |
This must be a boolean value, either “true” or “false” and prevents the layout from aligning its children’s baselines. |
android:id | This gives a unique id to the layout. |
例子
1.如何以垂直方式安排儿童观
XML格式
输出界面:
2.如何横向排列儿童视野
XML格式
输出界面:
3.如何使用layout_weight和weightSum
XML格式
输出界面:
4.如何使用重力
XML格式
输出界面:
5.如何使用layout_gravity
XML格式
输出界面: