android:layout_gravity="center",Android LinearLayout中子控件layout_gravity="center"无效 问题解决...

LinearLayout中子控件layout_gravity="center"无效 问题解决

在一些情况下,两个子控件,其中一个需要居中,另外一个在这个控件的后边

当时理所当然的默认使用了LinearLayout布局,想着设置成android:orientation="horizontal",然后其中一个控件居中即可

万万没想到居然不可以

代码如下:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/text1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:layout_marginEnd="4dp"

android:text="测试第一段"

android:textColor="@android:color/background_light"

android:textSize="22sp" />

android:id="@+id/text2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="4dp"

android:text="测试第二段"

android:textColor="@android:color/background_light"

android:textSize="22sp" />

579319b83d05

失败示例.png

发现可能是由于LinearLayout布局设置成横置后,控件再设置成居中,就直接失效了,反正最后怎么调我也没有成功,最终换成了RelativeLayout布局

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/text1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:layout_gravity="center"

android:layout_marginEnd="4dp"

android:text="测试第一段"

android:textColor="@android:color/background_light"

android:textSize="22sp" />

android:id="@+id/text2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="4dp"

android:layout_toEndOf="@id/text1"

android:text="测试第二段"

android:textColor="@android:color/background_light"

android:textSize="22sp" />

579319b83d05

成功示例.png

使用RelativeLayout布局后设置居中就可以了

感谢

如果对你有用,请点个爱心给个赞吧~~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值