相对布局android:visibility,Android Day01-布局详解

在Android中布局分为线性布局、相对布局、帧布局、表格布局、网格布局、绝对布局,常用的是线性布局和相对布局。

**布局本身也是一个组件(通过类的继承关系可以得知),布局文件中不管是什么组件包括布局本

身要指定layout_width和layout_height属性。

**布局上的属性是相互影响的,有着优先级。如weight,如果设置了布局的这个属性。组件的

layout_width或layout_height就要设置为0.

一、线性布局

uorientation属性是指定线性布局的排列方向:

ühorizontal 水平(默认)

üvertical 垂直

ugravity属性是指定当前控件内容显示位置:

üleft左边

üright右边

ütop上边

übottom底边

ulayout_gravity属性是指定当前控件在父元素的位置:

üleft左边

üright右边

ütop上边

übottom底边

ulayout_weightSum(权重)属性是把线性布局中剩余空间分成N份.

ulayout_weight (权重)属性是指定当前控件在父元素(线性布局)中占N份.

uvisibility属性是控制布局是否显示:

üvisible显示

üinvisible不显示但占空间

ügone隐藏

二、相对布局

位置、对齐

//属性值为@id/控件id名

uandroid:layout_toRightOf在指定控件的右边

uandroid:layout_toLeftOf  在指定控件的左边

uandroid:layout_above  在指定控件的上边

uandroid:layout_below  在指定控件的下边

uandroid:layout_alignBaseline  跟指定控件水平对齐

uandroid:layout_alignLeft  跟指定控件左对齐

uandroid:layout_alignRight  跟指定控件右对齐

uandroid:layout_alignTop  跟指定控件顶部对齐

uandroid:layout_alignBottom  跟指定控件底部对齐

//属性值为true或者false

uandroid:layout_alignParentLeft  是否跟父布局左对齐

uandroid:layout_alignParentTop  是否跟父布局顶部对齐

uandroid:layout_alignParentRight  是否跟父布局右对齐

uandroid:layout_alignParentBottom  是否跟父布局底部对齐

uandroid:layout_centerVertical  在父布局中垂直居中

uandroid:layout_centerHorizontal  在父布局中水平居中

uandroid:layout_centerInParent  在父布局中居中

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/id_btn01"

android:text="Btn02"

android:layout_alignParentRight="true"

/>

layout_toRightOf与layout_alignParentRight="true"同时存在,按钮的宽度就等于match_parent了。

c2a226788fe4d390e9dcee16719b0257.png

易于混淆的属性

1.layout_marginXXX与layout_paddingXXX的区别

padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。

margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离,如

果同一级只有一个view,那么它的效果基本上就和padding一样。(网上经常有说margin是相对于

父控件,这种说法其实是错误的。)

如下面的是垂直排列LinearLayout中的两个TextView控件,layout_margintop属性分别为50和

100。因为是垂直排列,所以下面的那个TextView的top是相对于上一个控件的。效果图如下所示。

1bee31520dae228932c32919bcb3f99a.png

如果将LinearLayout的方向改为水平排列的话,两个TextView的top都是相对于父窗体的。效

果图如下:

b4625e8404e32552f5d5c85ea112d357.png

三、ConstraintLayout对通知栏的RemoteView的兼容性问题

如果 RemoteView的布局使用了ConstraintLayout,则会报下面的错误:

Bad notification posted from packag xxxxx Couldn't expand RemoteViews for: StatusBarNotificationmargin设置无效

例如:android:layout_marginLeft="16dp",若想要其生效,需加上:app:layout_constraintTop_toTopOf="parent",其他margin属性类似

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值