LinearLayout match_parent wrap_content layout_weight

本文通过四种不同情况探讨了Android中LinearLayout布局的特性,特别是在水平方向上使用match_parent与wrap_content属性时的行为差异。通过实例展示了子视图如何受父布局宽度属性的影响。

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

This is my MainActivity layout: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.custom.app002.MainActivity"
    android:orientation="vertical">

    <!--情况1:不使用weight,所有的子视图的layout_width="wrap_content"-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/red"
            android:text="Hello World!"
            android:textColor="@color/white"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/blue"
            android:text="Hello World!"
            android:textColor="@color/white"/>
    </LinearLayout>
    <!--情况2:不使用weight,View1的layout_width="match_parent"-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="10dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/red"
            android:text="Hello World!"
            android:textColor="@color/white"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/blue"
            android:text="Hello World!"
            android:textColor="@color/white"/>
    </LinearLayout>
    <!--情况3:不使用weight,所有子视图的layout_width="match_parent"-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/red"
            android:text="Hello World!"
            android:textColor="@color/white"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/blue"
            android:text="Hello World!"
            android:textColor="@color/white"/>
    </LinearLayout>
    <!--情况4:不使用weight,view2的layout_width="match_parent"-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/red"
            android:text="Hello World!"
            android:textColor="@color/white"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/blue"
            android:text="Hello World!"
            android:textColor="@color/white"/>
    </LinearLayout>
</LinearLayout>

下面是运行效果:

看到这个效果的时候我也感觉到很疑惑,为什么第二个和但三个LinearLayout这么高呢?

经过多次的实验中央找到原因:第二个TextView并不是被挤出了,或者说是消失,而是被挤成一条线


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/et_zujian_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入组件名称"/> <EditText android:id="@+id/et_bancai_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入板材名称(可选)" android:layout_marginTop="8dp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="材质选择" android:layout_marginTop="16dp"/> <Spinner android:id="@+id/spinner_caizhi" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="木皮1" android:layout_marginTop="8dp"/> <Spinner android:id="@+id/spinner_mupi1" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="木皮2(可选)" android:layout_marginTop="8dp"/> <Spinner android:id="@+id/spinner_mupi2" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> // 添加木皮预览标签 TextView previewMupi1 = dialogView.findViewById(R.id.tv_preview_mupi1); TextView previewMupi2 = dialogView.findViewById(R.id.tv_preview_mupi2);
最新发布
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值