Android——四种基本布局
1.线性布局LinearLayout
android:orientation="horizontal" > android:layout_weight="1" android:hint="Type something" />
属性解释:
android:orientation="horizontal"//控件水平排列
android:orientation="vertical"//控件垂直排列
android:layout_weight="1"//若多个控件用layout_weight,则按此所占总和宽度的比例排布。此例只一个控件用此属性,则horizontal水平方向占满Button剩下的全部比例。
2.相对布局RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
这些按钮通过父布局Parent进行定位,效果图如下: 另外,如果要指定控件垂直或水平方向在整体布局中的对齐方式(要先指定了水平对齐horizontal或垂直对齐vertical),可用android:layout_gravity属性。
android:layout_gravity="top"
android:layout_gravity="center_vertical"
android:layout_gravity="bottom"
android:layout_above="@id/button3" android:layout_toLeftOf="@id/button3" android:text="Button 1" /> android:layout_above="@id/button3" android:layout_toRightOf="@id/button3" android:text="Button 2" /> android:layout_below="@id/button3" android:layout_toLeftOf="@id/button3" android:text="Button 4" /> android:layout_below="@id/button3" android:layout_toRightOf="@id/button3" android:text="Button 5" />
另外四个按钮都相对于Button3定位,效果图如下:
3.框架布局FrameLayout
所有控件都会摆放在布局的左上角,即使使用了定位的属性(例如android:layout_below="@id/button3" android:layout_toLeftOf="@id/button3"也会失效)
效果如下:
五个按钮都堆叠在了左上角(后堆的覆盖在先堆的上面)。
4.表格布局TableLayout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
注意:TableRow内的控件不能指定宽度。 每加入一对
此例为三行两列的表格,其中第三行只有一个Button按钮,因此使用layout_span="2"对单元格合并,效果如下:
然而此时表格右边空出一片没填满,而TableRow内控件又不能指定宽度,可加一句android:stretchColumns="x"意思为当行宽度未填满时可自动拉伸第x+1列。
例如刚刚的例子要拉伸第二列,即编辑框EditText,则加android:stretchColumns="1"
android:stretchColumns="1" >
效果如下: 总结一下,四种基本布局有:
1.线性布局LinearLayout
2.相对布局RelativeLayout
3.框架布局FrameLayout
4.表格布局TableLayout
更多相关文章
- Android(安卓)TextView属性大全
- TextView控件的使用(Android设置文本显示格式)
- android_控件属性
- Android中的android:layout_width和android:width
- Android中的android:layout_width和android:width
- ListView点击事件不响应。
- android listview 控件触摸滑动出现白屏问题
- Android中的android:layout_width和android:width
- android常用控件