android布局gravity,android布局RelativeLayout中android:gravity="center_horizontal"和android:layout_centerHo...

RelativeLayout中设置居中的方法有两个,一个是android:gravity="center_horizontal"还有一个是 android:layout_centerHorizontal="true"

那么他们有啥区别呢?

首先:android:gravity="center_horizontal"

android:layout_width="match_parent"

android:layout_height="75dp"

android:gravity="center_horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="账号:12345678" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="密码:1234" />

效果:

a317ca67803d

图片.png

然后是:android:layout_centerHorizontal="true"

android:layout_width="match_parent"

android:layout_height="75dp" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:text="账号:12345678" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:text="密码:1234" />

效果:

a317ca67803d

图片.png

所以得出结论:

1.RelativeLayout的实际内容的宽高根据所有子视图中的最大宽高所决定;

2.对于android:gravity="center_horizontal",对于自身的内容是水平居中的,但并不会设置其中所有子视图的对齐值;

3.对于android:layout_centerHorizontal="true",则只是针对子视图自身水平居中于父视图;

4.例子中显然android:gravity="center_horizontal"的效果比较好,因为既可以做到子视图居中,又可以让子视图对齐;

5.用哪个方法根据实际情况而定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值