关于android:layout_weight的一点心得

本文介绍了一种通过调整Android布局中元素的权重来解决布局显示不全的问题。具体做法是在GridLayout前添加权重属性,使剩余空间留给后续元素,确保所有控件都能正确显示。

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@android:color/black"
        android:layout_weight="1">           
        <GridView android:id="@+id/gv"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:numColumns="2">
     	</GridView>    
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical|center_horizontal"
        
        >
        <Button android:text="完成" android:layout_width="100dp" android:layout_height="wrap_content"></Button>
        <Button android:text="取消" android:layout_width="100dp" android:layout_height="wrap_content"></Button>
    </LinearLayout>
</LinearLayout >

这个weight应该翻译为权重,如下图,gridview是fill_parent,按钮所在的LinearLayout是wrap_content,因为gridview写在前面,所以优先占用了所有空间,导致按钮不显示

所以把gridview加上一个android:layout_weight="1",权重由0变为1表示它的权重下降,所以按钮优先布局


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值