500字范文,内容丰富有趣,生活中的好帮手!
500字范文 > Android常用布局-线性布局和相对布局

Android常用布局-线性布局和相对布局

时间:2021-05-06 20:07:19

相关推荐

Android常用布局-线性布局和相对布局

1. 线性布局 LinearLayout

线性布局分为水平和垂直两个方向,优点是简单可嵌套,缺点是只能在一个方向进行布局,而且嵌套过多可能存在风险

1.标签:<LinearLayout />

2.基本属性

id:表示当前布局或控件的唯一标识,构建后自动在R.java文件中生成一串标识符,可作为查找和引用控件的参考。

layout_width:表示当前布局的宽度,可以使用match_parentwrap_content来表示当前的布局大小,或直接使用dp值固定大小。

layout_height:表示当前布局的高度,可以使用match_parentwrap_content来表示当前的布局大小,或直接使用dp值固定大小。

orientation:表示当前布局的分布方向,分为水平方向horizental和垂直方向vertical,如果没有定义此属性可能会报错。

background:表示当前布局的背景,可以用十六进制的RGB、ARGBRRGGBB、AARRGGBB来表示,也可以调用drawble文件夹当中的图片作为背景。

padding: 表示当前布局内部空间的填充值,一般使用dp来作为衡量单位,线性布局从左上角开始,也可以选择方向paddingTop,paddingLeft,paddingRight,paddingBottom

margin: 表示当前控件或布局距离周围空间布局或父布局的距离,可选方向为margin_left,margin_top,margin_bottom,margin_right

layout_weight:表示当前控件或布局中子控件所占权重,按数字来进行划分。

在划分权重时按当前数字/数字总和作为权重占比,Eg. 两个子控件权重都为1则各占1/2。且一般不预先分配固定大小。

8.gravity:表示当前控件中子控件的对齐方式,因此此属性在父容器的标签中出现。可取值为:bottom\center\top\center_vertical\center_horizontal

layout_gravity:表示当前控件位于父控件中的对其方式,取值为center\top\bottom等。

3.代码实例

<?xml version="1.0" encoding="utf-8"?><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"xmlns:android="/apk/res/android"android:orientation="horizontal"><LinearLayoutandroid:orientation="horizontal"android:id="@+id/llout1"android:layout_width="200dp"android:layout_height="200dp"android:background="#FF123456"android:gravity="center|bottom"android:layout_gravity ="center_vertical"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"></Button></LinearLayout><LinearLayoutandroid:orientation="vertical"android:id="@+id/llout2"android:layout_width="200dp"android:layout_height="200dp"android:background="#FF654321"android:layout_marginTop="50dp"android:layout_gravity ="bottom"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"></Button></LinearLayout></LinearLayout>

2. 相对布局 RelativeLayout

相对布局从屏幕的左上角开始绘制,并且堆叠排列。优点是自由度比较高,缺点是需要大量的位置参照。

1.标签: <RelativeLayout />

2.基本属性

id:表示当前布局或控件的唯一标识,构建后自动在R.java文件中生成一串标识符,可作为查找和引用控件的参考。

layout_width:表示当前布局的宽度,可以使用match_parentwrap_content来表示当前的布局大小,或直接使用dp值固定大小。

layout_height:表示当前布局的高度,可以使用match_parentwrap_content来表示当前的布局大小,或直接使用dp值固定大小。

background:表示当前布局的背景,可以用十六进制的RGB、ARGBRRGGBB、AARRGGBB来表示,也可以调用drawble文件夹当中的图片作为背景。

padding: 表示当前布局内部空间的填充值,一般使用dp来作为衡量单位,线性布局从左上角开始,也可以选择方向paddingTop,paddingLeft,paddingRight,paddingBottom

margin: 表示当前控件或布局距离周围空间布局或父布局的距离,可选方向为margin_left,margin_top,margin_bottom,margin_right

layout_alignParent: 表示当前的空间位于父控件的分布位置 例:

<layout_alignParentBottom = “true”>

layout_toRightof = “@id+”:表示当前控件位于id值容器的右侧,同时可替换为top\bottom\left\right等

3.代码实例

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".RelativeActivity"><Buttonandroid:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="button1"android:layout_centerInParent="true"></Button><Buttonandroid:id="@+id/btn2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="button2"android:layout_toLeftOf="@+id/btn1"android:layout_above="@id/btn1" ></Button><Buttonandroid:id="@+id/btn3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="button3"android:layout_toRightOf="@+id/btn1"android:layout_below="@id/btn1"></Button><Buttonandroid:id="@+id/btn4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="button4"android:layout_toLeftOf="@id/btn1"android:layout_alignTop="@id/btn1"></Button></RelativeLayout>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。