android相对布局讲解 android相对布局属性


android相对布局讲解 android相对布局属性

文章插图
有哪些布局类型?
Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局) 。其中最常用的的是LinearLayout、TablelLayout和RelativeLayout 。这些布局都可以嵌套使用 。
LinearLayout(线性布局)
线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后 。线性布局分为两种:水平方向和垂直方向的布局 。分别通过属性android:orientation=”vertical” 和 android:orientation=”horizontal”来设置 。
案例代码分析:
【android相对布局讲解 android相对布局属性】<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:orientation="vertical" android:gravity="center_vertical|center_horizontal" tools:context="zzxb.me.layoutdemo.MainActivity"> <Button android:id="@+id/linearLO" android:text="@string/linear_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/tableLO" android:text="@string/table_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/frameLO" android:text="@string/frame_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/relativeLO" android:text="@string/relative_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/absLO" android:text="@string/abslayout_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>线性布局是用<LinearLayout>标签标示,其中常用的属性:
layout_width/layout_height:设置宽度和高度,其值有:wrap_content(适配内容大小),match_parent(适配父容器大小),此两个属性在各个控件中为通用属性
id:唯一标识该控件值
orientation:设置该布局是水平布局(horizontal)还是纵向布局(vertical)
gravity:设置控件的对齐方式,常用值:center_vertical(纵向居中)|center_horizontal(水平居中)
在<Button>标签中,也同样有id,layout_width以及lay_height属性 。同时,还有如下常用属性:
text:设置按钮文字,这里有两种方式,一种是直接硬编码,即直接写内容,例如:
android:text="按钮"第二种方式是非硬编码方式,是通过配置strings.xml文件来配置,例如:
<resources> <string name="btnText">按钮</string></resources>然后,通过:
android:text="@string/btnText"引用 。
页面跳转的方式:
Intent intent = new Intent(); intent.setClass(MainActivity.this,LinearActivity.class); startActivity(intent);TableLayout(表格布局)
表格布局与常见的表格类似,以行、列的形式来管理放入其中的UI组件 。表格布局使用<TableLayout>标签定义,在表格布局中,可以添加多个<TableRow>标签占用一行 。由于<TableRow>标签也是容器,所以还可以在该标签中添加其他组件,每添加一个组件,表格就会增加一列 。在表格布局中,列可以被隐藏,也可以被设置为伸展的,从而填充可利用的屏幕空间,还可以设置为强制收缩,直到表格匹配屏幕大小 。


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: