Android的UI布局以及常用属性

来源:互联网 发布:淘宝网最新版本 编辑:程序博客网 时间:2024/06/18 05:10

#### Android视图相关概念

View:是Android环境下所有视图的超类,它主要描述屏幕上一个矩形区域(有宽、高、背景、边距、显示隐藏之类的公共特性,拥有基本的触摸响应)

 

ViewGroup:继承自View对View有管理工具,可以将其子View布置到具体位置的功能

 

ViewGroup可以有子视图,View(非ViewGroup子类)表示的控件没有子视图

##### 基本属性

宽度 android:layout_width

高度 android:layout_height

宽高的值match_parent表示铺满父容器

wrap_content表示根据内容大小来调整宽高,wrap_content放在宽上表示内容有多宽它就有多宽,

在高上也如此

 

宽高也可以设置具体的尺寸,单位用dp、dip

##### FrameLayout

帧布局:特性——>层默认堆叠在左上角的方式

可以对内部的孩子使用android:layout_gravity设置位置

android:layout_gravity="right|bottom"

 

注意跟android:gravity属性的区别:

android:gravity属性表示该视图调整它内部内容的对齐方式

android:layout_gravity作用在自身调整自身在父容器中的位置

 

##### 公共属性

任何视图都有宽高,边距,背景这样的公共属性

外边距

android:layout_margin 四周外边距

android:layout_marginLeft 左外边距

android:layout_marginRight 右外边距

android:layout_margin...

 

内边距

android:padding  四周内边距

android:paddingLeft、Right、Top、Bottom设置各个方位的内边距

 

##### LinearLayout

线性布局:水平或者垂直方向来编排内部的元素

 

android:orientation="vertical或者horizontal"  垂直或者水平

如果不加默认为水平

android:layout_weight表示布局的权重,可以划分剩下的宽或者高实现铺满、均分这样的效果

权重的另一个概念:控制绘制顺序0,1,2,3...

#### RelativeLayout

相对布局

 

第一类相对:子视图相对于父容器,这类相对叫外相对,取值true/false

 

android:layout_centerHorizontal 水平居中

android:layout_centerVertical 垂直居中

android:layout_centerInParent 居中(水平+垂直居中)

android:layout_alignParentLeft\Right\Top\Bottom

第二类相对:子控件之间的相对,被参考的视图要有id,引用id方法"@id/id_name"

 

android:layout_above 在谁的上面

android:layout_below 在谁的下面

android:layout_toLeftOf 在谁的左边

android:layout_toRightOf 在谁的右边

android:layout_alignLeft\Top\Right\Bottom跟谁左、顶部、右、底部对齐

 

#### 布局技巧(复用性)

<include>标签,可以导入已经写好的布局

<include layout="@layout/title_layout" />

 

<merge>可以对导入的布局直接过滤掉,让其子控件直接添加到父容器

<merge xmlns:android="http://schemas.android.com/apk/res/android"

     android:layout_width="match_parent"

     android:layout_height="wrap_content" >

...这其中的视图会直接引用他父容器的布局属性

</merge>

merge标签可以放在跟布局上,然后导入到其他布局,或者添加到系统布局中



原创粉丝点击