LinearLayout
orientation屬性
horizontal和vertical:通過horizontal控制LinearLayout中的控件水準排列,設定成該屬性後,内部控件的寬度屬性不能再設為match_parent,否則它将會将整個水準控件占滿,同道理LinearLayout的排列方向是vertical,内部控件就不能将高度指定為match_parent。
示例效果如圖:
layout_gravity屬性
用于調整控件的對齊方式,代碼及示例樣式如下:
<Button
android:id="@+id/button4"
android:layout_width="133dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="top"
/>
<Button
android:id="@+id/button2"
android:layout_width="133dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center_vertical"
/>
<Button
android:id="@+id/button3"
android:layout_width="131dp"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="bottom"
/>
Layout_weigth屬性
可用于調整控件之間的占比
代碼及示例如下:
<EditText
android:id="@+id/input_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type something here" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Send"
可以看到兩個控件的layout_weight都置為“1”,則每個占1/2,若分别改成3和2,則變化如下:
但一般将button的layout_width置為"wrap_content",會更加雅觀!