【摘要】
这里主要对Android常用布局进行总结,简要阐述其基础用法和属性。包括Android六大布局:LinearLayout
(线性布局),RelativeLayout (相对布局),FrameLayout (帧布局),TableLayout
(表格布局),GridLayout (网格布局),AbsoluteLayout (绝对布局)。
注:这是本人学习Android过程中的一些总结,纰漏之处,还请指正!
本文一切知识点均参考自菜鸟教程,详细介绍可见“http://www.runoob.com”和"https://developer.android.google.cn/reference/"
本文适合Android初学者,大佬勿扰!
一、LinearLayout 线性布局
1、基础属性
2、weight使用详解:
- 当weight对应默认宽度设为 0dp 和 wrap_content 时,按权重比例实际宽度来设置
<?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:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#AAFFFF"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#DD7766"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#BB00CC" />
</LinearLayout>

此时的实际比例明显为:1:2:3
-
当weight对应默认宽度设置为 match_content或者fill_content时,实际比例 需要按以下方式计算
例如:将以上代码中的
android:layout_width="0dp"
全部改写为:
android:layout_width="fill_content"
此时,比例变为2:1:0 如下图:
此时比例计算应当按照如下方式:
step1: 最小权重-最大权重=最大权重负差,即1-3=-2; 权重和 1+2+3=6
step2: 每个组件实际权重 = 1+最大权重负差*(权重值/权重和),
即: 实际权重比例为:1-2*(1/6) : 1-2*(2/6) : 1-2*(3/6),即:2:1:0
3、divider使用详解
- 方法一:通过View设置简单的分割线
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000"/>

- 方法二:利用divider来设置
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="MainActivity"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:divider="@drawable/divider"
android:showDividers="middle"
android:dividerPadding="10dp">
...
</LinearLayout>
4、linearLayout注意事项
当 android:orientation="vertical" 时,只有水平方向的设置才起作用,垂直方向的设置不起作用.即:left,right,center_horizontal 是生效的;
当 android:orientation="horizontal" 时,只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。
二、RelativeLayout 相对布局
1、基础属性
三、TableLayout 表格布局
1、确定行数和列数
行:默认TableLayout 子控件均占满一行(即 layout_width 默认 fill_parent)
列:可通过添加Tablerow来在一行上添加多列
列数:即Tablerow中组件最多时的个数;
列宽:列宽由该列中最宽的单元格决定。
2、常用属性:
android:coolapseColums:需要被隐藏的列序号
android:shrinkColums:允许被收缩的列序号
android:strechColums:允许被拉伸的列序号
举例如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center_vertical"
android:background="#AACCFF"
android:stretchColumns="0,3">
<TableRow>
<TextView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textStyle="bold"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"/>
<TextView/>
</TableRow>
<TableRow>
<TextView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textStyle="bold"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"/>
<TextView/>
</TableRow>
<TableRow>
<TextView/>
<Button
android:id="@+id/button_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
android:textAppearance="?android:attr/textAppearanceButton"/>
<Button
android:id="@+id/button_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"
android:textAppearance="?android:attr/textAppearanceButton"/>
<TextView/>
</TableRow>
</TableLayout>

四、Framelayout 帧布局
这个布局方式几乎不会用到,这里只是简单提一下
- 简介
布局方式:帧布局的控件一般总是会在父容器的左上角进行叠加 - 基本属性
android:foreground:设置帧布局的前景图像
android:foregroundGravity:设置前景图显示的位置
五、GridLyout 网格布局
六、AbsoluteLayout 绝对布局
常用属性:
android:layout_width:组件宽度
android:layout_height:组件高度
android:layout_x:设置组件的X坐标
android:layout_y:设置组件的Y坐标
(几乎不会用到)