Android 权重 未知高度,BUG:使用android:layout_weight权重导致对齐出错

博客围绕Android纯布局问题展开,代码中使用布局权重但无法实现顶部或底部对齐,仅能居中。尝试将padding和margin设为0、添加布局位置对齐均失败,最终解决方案是在外层linearlayout添加android:baselineAligned=\false\属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

纯布局问题:代码如下:

android:id="@+id/linear_bottom"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#000" >

android:layout_width="fill_parent"

android:layout_height="100dp"

android:background="#f00"

android:orientation="horizontal" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#fff"

android:text="1111"

android:textSize="40dp" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#fff"

android:text="2222"

android:textSize="30dp" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="#fff"

android:text="3333"

android:textSize="10dp" />

效果图:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

结果无法顶部对齐,或底部对齐,只可以居中。

方案一:将padding和margin全部设为0,失败

android:padding="0dp"android:layout_margin="0dp"

方案二:添加布局位置对齐,失败

android:layout_gravity="top"  android:gravity="top"

解决方案:

外层linearlayout添加属性  android:baselineAligned="false",若无则导致了后面的两个textview已第一个textview为基准线, 导致设置android:layout_gravity="bottom"或者top时后面的两个textview没有对齐

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值