<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/ture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"/>
</LinearLayout>
</LinearLayout>
android:gravity 用来设置该控件里的子控件的位置。如图。
如果在Button控件里设置这个属性 就会控制Button控件里的文字位置,
android:layout_gravity 用来设置该控件在其父控件里的位置。
如果在Button控件里设置这个属性 就会控制Button控件在LinearLayout中的位置。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="@string/question_text"
/>
<LinearLayout
android:layout_width="match_parent"//将宽度匹配父类
android:layout_height="wrap_content"
android:orientation="vertical">//设为垂直排列
<Button
android:id="@+id/ture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/false_button"/>
</LinearLayout>
</LinearLayout>
由于是线性布局 所以设第二个LinearLayout为垂直排列 才能看出效果
线性布局将它所包含的控件在线性方向上依次排列。
如果为水平排列(horizontal),一个控件就会占一列,一列一列一次排下去,并且内部的控件绝不能将宽度设为match_parent。
如果为垂直排列(vertical),一个控件就会占一行,并且内部的控件不能将宽度设为match_parent。
而控件的layout_gravity属性在LinearLayout布局中,只能设置在它占的那一列(或者那一行)的位置。如图。