android:layout_weight的简单使用

本文详细介绍了Android中LinearLayout的布局权重属性layout_weight的应用方法。通过三个具体的例子展示了如何使用权重属性来控制视图间的空间分配,包括等比例分配、指定比例分配及固定与剩余空间分配的情况。

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

weight的意思是权重,在此处可以理解为比例。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#ac1a1a"
            android:text="1"
            android:textSize="50sp">

        </TextView>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#28469e"
            android:text="1"
            android:textSize="50sp">

        </TextView>

    </LinearLayout>
    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#ac1a1a"
            android:text="1/3"
            android:textSize="50sp">

        </TextView>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="#28469e"
            android:text="2/3"
            android:textSize="50sp">

        </TextView>

    </LinearLayout>

    <LinearLayout
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:background="#ac1a1a"
            android:text="200dp"
            android:textSize="50sp">

        </TextView>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#28469e"
            android:text="剩余的"
            android:textSize="50sp">

        </TextView>

    </LinearLayout>

</LinearLayout>

第一个LinearLayout里的两个textview按1:1分配宽度。

第二个LinearLayout里的两个textview按1:2分配宽度。

第三个LinearLayout里的两个textview一个是固定宽度,另一个占满剩余的宽度。

 

转载于:https://www.cnblogs.com/silenceshining/p/5383470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值