Android五种布局

1. LinearLayout

LinearLayout是线性布局控件,它包含的子控件将以横向或纵向排列。

注:布局之间可以嵌套使用。即布局内既可包含控件,又可包含布局。

两个全局属性:

1. android:orientation --决定其子类控件排布方式

android:orientation="horizontal" --水平排布
android:orientation="vertical" --垂直排布

2. android:gravity --决定子类控件位置

android:gravity="bottom" --底部
android:gravity="bottom|center_horizontal"--标签可连用(注:竖线左右不能加空格,需要注意逻辑)

--gravity几个属性值:
center_horizontal  --水平居中
center_vertical  --垂直居中
center  --水平垂直都居中
right  --子类控件位于当前布局的右边
left  --子类控件位于当前布局的左边
bottom  --底部
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

子控件常用属性:

1. android:layout_gravity= "bottom"  --控件本身在当期父容器的位置
2. android:layout_weight= "n"  --指本身控件占当前父容器的一个比例(n为数字)
  • 1
  • 2

weight属性示例代码:

<EditText
    android:id="@+id/text_1"
    android:layout_width="0dp" --注意此处为0dp(勿忘单位)
    android:layout_weight="1" --weight
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/button1"
    android:layout_width="0dp" --注意此处为0dp(勿忘单位)
    android:layout_height="wrap_content"
    android:layout_weight="1" --weight
    android:text="send" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

注意:两个控件的layout_width属性均为”0dp”,也说明有layout_weight属性的时候,layout_width属性不起作用。而此处指定为0是一种比较规范的写法。

效果如图所示: 

注:系统会先把 LinearLayout下所有控件指定的layout_weight 相加,得到一个总值,然后每个控件所占大小的比例就是用该控件的layout_weight 值除以刚才算出的总值。

若代码改为:

<EditText
    android:id="@+id/text_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="send" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

则效果如下图所示: 
 
这样看起来更加舒服,而且在各种屏幕上的适配效果会更好。

注意: 
1. gravity是用于指定文字在控件中的对齐方式;而layout_gravity是用于指定控件在布局中的对齐方式。 
2. 当LinearLayout的排列方向是 horizontal时,只有垂直方向上的对齐方式才会生效。因为此时水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。同理,当 LinearLayout 的排列方向是vertical时,只有水平方向上的对齐方式才会生效。

2. RelativeLayout

RelativeLayout是相对布局控件,它包含的子控件将以控件之间的相对位置或者子类控件相对父类容器的位置的方式排列。 
子类控件相对父类容器:

android:layout_margin="40dp" --子控件距父容器的边距
android:layout_marginLeft="32dp" --子控件距父容器的左边距
android:layout_marginTop="40dp" --子控件距父容器的顶边距

android:layout_alignParentLeft="true" --子控件相对当前父容器靠左边
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true" --子控件相对父容器水平居中
android:layout_centerVertical="true"
android:layout_centerInParent="true" --水平、垂直都居中
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

子类控件相对子类控件:

android:layout_below="@+id/bt1" --给定标签的底部
android:layout_above="@+id/bt1" --上面
android:layout_toRightOf="@+id/bt1" --右边

android:layout_alignBaseline="@+id/bt1" --该控件与给定控件内容在一条线上
android:layout_alignTop="@+id/bt1" --该控件与给定控件顶部对齐
android:layout_alignBottom="@+id/bt1"
android:layout_alignLeft="@+id/bt1" --该控件与给定控件左边缘对齐
android:layout_alignRight="@+id/bt1"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3. FrameLayout

FrameLayout是帧布局中,所有子元素都不能被放置指定位置,默认放在布局左上角。且后面的子元素直接覆盖在前面的子元素之上,可将前面的子元素部分或全部遮挡。 
如图所示: 
 

几个属性:

android:foreground="" --设置前景图 ,在所有子视图的前面
android:foregroundGravity="" --设置前景图位置

android:layout_gravity="center" --设置居中
android:background= "#999999" --设置背景

android:keepScreenOn="true" --保持屏幕唤醒
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4. TableLayout

TableLayout表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。

如图所示: 

TableLayout全局属性:

android:collapseColumns="n" --隐藏第 n 列
android:stretchColumns= "m, n" --拉伸第 m,n 列
android:shrinkColumns= "n" --压缩第 n 列
  • 1
  • 2
  • 3

注意:索引从0开始,多个之间用逗号分隔( * 号表示所有列)

TableLayout局部属性:

android:layout_column= "n" --该控件占据第 n 列(注意:从第0列开始算起)
android:layout_span= "n" --该控件占用 n 列
  • 1
  • 2

注:TableRow 中的控件不能指定宽度。

5. AbsoluteLayout

AbsoluteLayout是绝对布局,又可叫坐标布局,可直接指定子元素的绝对位置(xy坐标)。由于手机屏幕尺寸差别比较大,故绝对布局适应性差。因此该布局方式用的较少。了解即可。

时间: 2024-08-01 16:37:48

Android五种布局的相关文章

Android五种布局方式——LinearLayout、RelativeLayout、TableLayout....(四)

Android五种布局方式--LinearLayout.RelativeLayout .TableLayout.... Android使用XML声明界面布局 将程序的表现层和控制层分离 修改用户界面时,无需更改程序的源代码 可视化工具设计用户界面 Android五种布局方式 LinearLayout线性布局 AbsoluteLayout坐标布局 RelativeLayout相对布局 FrameLayout帧布局 TableLayout表格布局 GridLayout 1.LinearLayout线

Android五种布局说明

AbsoluteLayout---->是一个按照绝对坐标定义的布局,由于使用绝对坐标去定位控件,因此要实现自适应界面时,应尽少使用 AbsoluteLayout . RelativeLayout---->最好在界面设计时 做好布局,尽少程序运行时 做控件布局的更改,因为 RelativeLayout布局里面的属性之间,很容易冲突 FrameLayout---->顾名思义跟帧有关,布局里所有的控件都被放到布局的左上角,并且一层覆盖一层. TableLayout + TableRow----

android五种布局模式

Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:LinearLayout (线性布局),FrameLayout(框架布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局). 一. 线性布局 线性布局.每一个LinearLayout里面又可分为垂直布局(android:orientation="vertical")和水平布局(android:orientation="

Android突击:常用的五种布局

1,FrameLayout FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡.显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二个TextView的部分位置. FrameLayout <?xml version="1.0" encoding=&

【转】Android UI 五种布局

在一个Android应用中,Layout是开发中的一个很重要环节,Layout是组成UI不可缺少的一部分. ## Android UI 核心类 在Android应用构建UI的方法有以下几种: 单纯使用JAVA代码 使用XML完全定义界面 结合使用两者,在XML中定义,在JAVA中引用和修改 Android SDK中关于UI的核心类: Android.view.View和android.view.ViewGroup android中的常见UI控件均会扩展View和ViewGroup其中有一部分是专

android五种数据存储方式

在Android中提供了如下五种数据存储方式: SharedPreferences 文件存储 SQLite数据库方式 内容提供器(ContentProvider) 网络存储 下面具体介绍一下这五种数据存储方式的用法. 1. SharedPreferences SharedPreferences是android平台上的一个轻量级存储类,主要保存一些常用的配置信息.可以保存long.int.String类型数据. SharedPreferences以键值对方式读取和存入,最终会以XML方式保存数据,

Android 五种存储方式个人总结

一 . 文件存储 FileOutputStream out = openFileOutput("data",Context.MODE_PRIVATE); BufferedWriter writer = new BufferedWriter(new OutputStream(out)); String  s ; writer.writer(s); 二. sharePreferences 存储 (键值对形式) 方法 1.    Context 类中的getSharedPreferences

五种布局方式

线性布局 LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列. LinearLayout本身常用到的两个属性 android:orientation="vertical"----该属性决定他子类控件的排布方式(vertical:垂直:horizontal:水平) android:gravity="center"----该属性决定他子类的xy的位置. 常用到的几个属性值: center_vertical:垂直(Y轴)居中 center_ho

android五个布局

此内容是根据在慕课网学习,加上对自己的理解,编写下来的笔记,以便以后查看 布局内可以再有布局 1.线性布局(LinearLayout) 此布局可以控制控件垂直/水平排列 LinearLayout常用到两个属性 1.android:orientation="vertical(垂直)"/"horizontal(水平)" 2.android:gravity = "center"---该属性控制它子类的位置(写在总的开头) (常用的属性有) 1.cent