【Android】Android中两种常用布局(LinearLayout和RelativeLayout)
2017-01-25 23:22
513 查看
LinearLayout
LinearLayout是一个很常用的布局,用于在单个方向(垂直或水平)中对齐所包含的所有组件,可以使用android:orientation属性指定布局方向。一个LinearLayout的所有组件都是一个接一个堆叠的,所以一个垂直列表每行只有一个组件,不管它们有多宽,水平列表只有一行高(最高组件的高度加padding)。 LinearLayout遵从组件之间的margin,和每个组件的gravity(右,中心或左对齐)
常用属性
orientation:布局中的排列方式,有两种,horizontal和verticalgravity:控制所包含组件的对齐方式,可组合(如 left|top)
layout_gravity:控制该组件在父容器中的对齐方式
layout_width:布局的宽度,可用wrap_content,用fill_content或match_parent填满父容器
layout_height:布局的高度,同上
id:为该组件设置一个id,在Java文件中可以通过findViewById(id)找到该组件
background:为该组件设置一个背景图片,或者用颜色填充
android:layout_weight
LinearLayout还支持使用android:layout_weight属性为各个组件分配权重。此属性根据应在屏幕上占据多少空间来为视图分配“重要性”值。较大的权重值允许扩展以填充父视图中的剩余空间。组件可以指定权重值,然后视图组中的剩余空间按其声明权重的比例分配给组件。默认权重为0。例如,如果有三个文本字段,其中两个声明权重为1,而另一个没有权重,则第三个没有权重的文本字段将不会增长,并且只占用其内容所需的区域。其他两个将均匀扩展,以填充测量所有三个字段后剩余的空间。如果第三个字段的权重为2(而不是0),那么它现在被声明比其他字段更重要,因此它占总剩余空间的一半,而前两个字段平均分配其余的空间。
等权重子项
要创建一个线性布局,其中每个组件在屏幕上使用相同的空间量,将每个视图的android:layout_height设置为“0dp”(对于垂直布局)或将每个视图的android:layout_width设置为“0dp”(对于水平布局)。 然后将每个视图的android:layout_weight设置为“1”。android:divider
可绘制为用作按钮之间的垂直分隔线。可能是对另一个资源的引用,形式为“@ [+] [package:] type:name”或以“?[package:] [type:] name”形式的主题属性。
可以是颜色值,形式为“#rgb”,“#argb”,“#rrggbb”或“#aarrggbb”。
这对应于全局属性资源符号分割器。
相关方法:setDividerDrawable(Drawable)
例子
布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="16dp" android:paddingRight="16dp"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Type something here" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Type something here" /> <EditText android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top" android:hint="message" /> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:text="send" android:textAllCaps="false" /> </LinearLayout>
RelativeLayout
RelativeLayout是一个以相对位置显示组件的布局。 每个组件的位置可以指定为相对于兄弟元素(例如,在另一个组件的左侧或下方)或相对于父级RelativeLayout区域的位置(例如,对齐到底部,左侧或中心)。RelativeLayout是一个非常强大的用于设计用户界面的实用布局,因为它可以避免使用嵌套视图组,并使布局层次结构保持平坦,从而提高性能。 如果你发现自己使用了多个嵌套的LinearLayout组,您可以用一个RelativeLayout替换它们。
定位组件
RelativeLayout允许组件指定它们相对于布局或彼此(由ID指定)的位置。 因此,你可以通过右边框对齐两个元素,或使一个在另一个下面,在屏幕中心,左中间,等等。 默认情况下,所有组件都绘制在布局的左上角,因此必须使用各种布局属性来定义每个视图的位置。RelativeLayout中可用于视图的布局属性中的一些包括:
android:layout_alignParentTop
如果为“true”,则使此视图的顶边与父视图的顶边对齐。
android:layout_centerVertical
如果为“true”,则将此子视图在其父视图内垂直居中。
android:layout_below
将此视图的顶部边缘定位在使用资源ID指定的视图下方。
android:layout_toRightOf
将此视图的左边缘置于使用资源ID指定的视图的右侧。
每个布局属性的值是布尔值,以启用相对于父RelativeLayout的布局位置或引用视图应位于的布局中的另一个视图的ID。
在XML布局中,可以以任何顺序声明对布局中其他视图的依赖性。 例如,可以声明“view1”位于“view2”下方,即使“view2”是层次结构中声明的最后一个视图。 下面的示例演示了这种情况。
例子
布局代码如下:
<?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" android:paddingLeft="16dp" android:paddingRight="16dp"> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Type something here" /> <Spinner android:id="@+id/dates" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/name" android:layout_toLeftOf="@+id/times" /> <Spinner android:id="@id/times" android:layout_width="96dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/name" /> <Button android:layout_width="96dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/times" android:text="Done" /> </RelativeLayout>
相关文章推荐
- Android课堂笔记——LinearLayout和RelativeLayout常用的两种布局方式
- Android常用的五大布局:LinearLayout/RelativeLayout/AbsoluteLayout/ TableLayout/FrameLayout
- Android 常用布局 LinearLayout+RelativeLayout+FrameLayout+ConstraintLayout
- android RelativeLayout常用xml布局属性
- 【Android】11、相对布局RelativeLayout常用属性
- Android相对布局RelativeLayout常用到的属性
- Android:控件布局(相对布局)RelativeLayout RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列。 相对布局常用属性: 子类控件相对子
- Android常用布局之LinearLayout之简易计算器
- Android Layout布局使用总结Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件。 帧布局(FrameLayout):组件从
- Android 布局(LinearLayout\TableLayout\RelativeLayout\AbsoluteLayout\FrameLayout)
- Android零基础入门第25节:最简单最常用的LinearLayout线性布局
- android RelativeLayout(相对布局)常用属性
- Android RelativeLayout布局之常用属性详解
- Android LinearLayout和RelativeLayout 布局2
- 更改android创建工程的布局RelativeLayout-->LinearLayout
- Android 布局(LinearLayout\TableLayout\RelativeLayout\AbsoluteLayout\FrameLayout)
- android 常用布局LinearLayout以及RelativeLayout介绍
- Android LinearLayout和RelativeLayout 布局1
- Android常用布局控件之RelativeLayout
- Android零基础入门第25节:最简单最常用的LinearLayout线性布局