Android(安卓)之 布局训练
16lz
2021-01-26
1.线性布局 LinearLayout
<?xml version="1.0" encoding="utf-8"?><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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="org.example.linearlayout.MainActivity"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/list_view" />LinearLayout>
package org.example.linearlayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapteritems = new ArrayAdapter (MainActivity.this, android.R.layout.simple_list_item_1); items.addAll("raspberry", "apple", "banana", "orange", "watermelon", "pear", "grape", "pineapple", "strawberry", "cherry", "mango"); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(items); }}
2.表格布局
常用属性:
TableLayout属性(也叫全局属性):*代表所有列android:shrinkColumns -------设置可收缩的列,(内容过多,则收缩,扩展到第二行,控件没布满TableLayout时不起作用)android:stretchColumns ------设置可伸展的列,(有空白则填充)列可以同时具备stretchColumns及shrinkColumns属性android:collapseColumns ------设置要隐藏的列(索引列从0开始) android:layout_column -------该单元格在第几列显示android:layout_span -------该单元格占据列数,默认为1
例:android:stretchColumns="0" 第0列可伸展android:shrinkColumns="1,2" 第1,2列皆可收缩android:collapseColumns="*" 隐藏所有行
表格布局示例
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TableLayout android:id="@+id/Table1" android:layout_width="match_parent" android:layout_height="wrap_content" android:shrinkColumns="1" android:stretchColumns="2"> <Button android:id="@+id/ok1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮占一行"/> <TableRow> <Button android:id="@+id/ok2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮"/> <Button android:id="@+id/ok3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收缩的按钮,可扩展到第二行"/> <Button android:id="@+id/ok4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按钮,可以拉伸"/> TableRow> <TableRow> <Button android:id="@+id/ok6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮"/> <Button android:id="@+id/ok7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮"/> <Button android:id="@+id/ok8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮"/> TableRow> <TableRow> <Button android:id="@+id/ok13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮" /> <Button android:id="@+id/ok14" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按钮,可以拉伸" /> TableRow> TableLayout>LinearLayout>
3.帧布局
常用属性:
android:foreground:设置改帧布局容器的前景图像 android:foregroundGravity:设置前景图像显示的位置
代码:
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/view1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|bottom" android:width="160pt" android:height="160pt" android:background="#f00"/> <TextView android:id="@+id/view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="140pt" android:height="140pt" android:background="#0f0"/> <TextView android:id="@+id/view3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:width="120pt" android:height="120pt" android:background="#00f"/> <TextView android:id="@+id/view4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:width="100pt" android:height="100pt" android:background="#ff0"/> <TextView android:id="@+id/view5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:width="80pt" android:height="80pt" android:background="#f0f"/> <TextView android:id="@+id/view6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="60pt" android:height="60pt" android:background="#0ff"/>FrameLayout>
4.相对布局
android:layout_marginBottom 离某元素底边缘的距离android:layout_marginLeft 离某元素左边缘的距离android:layout_marginRight 离某元素右边缘的距离android:layout_marginTop 离某元素上边缘的距离
android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘对齐,并置于父窗口最左边,会覆盖最左边的控件 android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐,并置于父窗口最上边,会覆盖最上边的控件 android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐,并置于父窗口最上边,会覆盖最上边的控件 android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐,并置于父窗口最左边,会覆盖最左边的控件android:layout_above 将该控件的底部至于给定ID的控件之上,但不会左对齐,默认置于父窗口最左边,会覆盖最左边的控件 android:layout_below 将该控件的顶部至于给定ID的控件之下,但不会左对齐,默认置于父窗口最左边,会覆盖最左边的控件 android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐,默认置于父窗口最上面,会覆盖最上面的控件 android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐,默认置于父窗口最上面,会覆盖最上面的控件
android:layout_alignBaseline该控件的baseline和给定ID的控件的baseline对齐,并置于父窗口最左边,会覆盖最左边的控件
android:layout_centerHorizonal="true" 水平居中android:layout_centerVertical="true" 垂直居中android:layout_centerInParent="true" 位于中央位置android:layout_alignParentBottom ="true" 控件的底部和父控件的底部对齐,默认置于父窗口最左下,会覆盖最左下的控件 android:layout_alignParentLeft="true" 控件的左边与父控件的左边对齐,默认置于父窗口最左上,会覆盖最左上的控件 android:layout_alignParentRight="true" 控件的右边与父控件的右边对齐,默认置于父窗口最右上,会覆盖最右上的控件 android:layout_alignParentTop="true" 控件的顶部与父控件的顶部对齐,默认置于父窗口最左上,会覆盖最左上的控件android:layout_alignParentStart="true" 控制该组件和布局管理器开始对齐android:layout_alignParentEnd="true" 控制该组件是否和布局管理器末端对齐。android:layout_centerHorizontal="true" 控件将被至于水平方向的中央(默认父布局的上方) android:layout_centerInParent="true" 控件将被至于父控件水平方向和垂直方向的中央 android:layout_centerVertical="true" 控件将被至于垂直方向的中央(默认父布局的左边)android:layout_alignWithParentIfMissing="true" 如果对应的兄弟元素找不到的话就以父元素做参照物
代码如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/view1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:width="30pt" android:height="30pt" android:layout_marginBottom="10pt" android:layout_marginLeft="10pt" android:layout_marginRight="10pt" android:layout_marginTop="10pt" android:background="#f00"/> <TextView android:id="@+id/view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/view1" android:layout_alignLeft="@id/view1" android:width="30pt" android:height="30pt" android:background="#0f0"/> <TextView android:id="@+id/view3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/view1" android:layout_alignLeft="@id/view1" android:width="30pt" android:height="30pt" android:background="#00f"/> <TextView android:id="@+id/view4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/view1" android:layout_alignTop="@id/view1" android:width="30pt" android:height="30pt" android:background="#ff0"/> <TextView android:id="@+id/view5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/view1" android:layout_alignTop="@id/view1" android:width="30pt" android:height="30pt" android:background="#f0f"/>RelativeLayout>
5.网格布局
android:layout_column 设置该子组件在GridLayout的第几列 android:layout_columnSpan 设置该子组件在GridLayout横向上跨几列 android:layout_gravity 设置该子组件采用何种方式占据该网格 android:layout_row 设置该子组件在GridLayout的第几行 android:layout_rowSpan 设置该子组件在GridLayout纵向上跨几行。android:alignmentMode 设置该布局管理器采用的对齐模式android:columnCount 设置该网格的列数量android:columnOrderPreserved 设置该网格容器是否保留列序号android:orientation 设置该网格的排列方向,"vertical" 为垂直分布,"horizontal"为水平分布android:rowCount 设置该网格的行数量android:rowOrderPreserved 设置该网格容器是否保留行序号android:useDefaultMargins 设置是否使用默认的页边距
代码如下:
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/lable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="20dip" android:layout_y="20dip" android:text="input" /> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_y="40dip" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="180dip" android:layout_y="100dip" android:width="60dip" android:text="确定" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="270dip" android:layout_y="100dip" android:width="60dip" android:text="取消" />AbsoluteLayout>
更多相关文章
- Android(安卓)View事件分发机制理解
- Android实现控件动画效果
- Android(安卓)日期控件选择
- Android(安卓)查看大图(直接复制可以用)
- android 下拉刷新控件
- Android(安卓)animated-rotate简单的图片旋转动画
- Android(安卓)控件GridView的使用
- android自定义适配屏幕的ImageView
- Android常用控件之RecyclerView