关于layout_weight="1"的用法

<?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="horizontal" >
    <TextView 
            android:id="@+id/text1"
            android:text="thired"
            android:background="#fff000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
        <TextView 
            android:id="@+id/text2"
            android:text="123456789123456789132456789"
            android:background="#449000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />

</LinearLayout>

    很多新手刚学的时候,总是以为layout_weight=“1”是子控件平分父控件的剩余空间。其实不然...

    应该说layout_weight是子控件的剩余空间平分父控件的剩余空间。如果有一个控件的文本占用位置很多的话,那么使用layout_weight=“1” 它的空间就会比另外一个大很多。


``` <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_F2E7DB"> <FrameLayout android:id="@+id/fit_status_bar" android:layout_width="match_parent" android:layout_height="53dp" android:alpha="0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:id="@+id/ll_search" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:paddingHorizontal="12dp" android:paddingVertical="12dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/iv_back" android:layout_width="40dp" android:background="@drawable/clickable_white" android:layout_height="40dp" android:padding="8dp" android:scaleType="centerCrop" android:src="@drawable/icon_back" /> <com.stary.cateyes.widget.SearchEditText android:id="@+id/et_search" android:layout_width="0dp" android:layout_weight="1" android:layout_height="32dp" android:layout_marginEnd="15dp" android:layout_marginStart="@dimen/dp_4" /> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/open_window" android:background="@drawable/icon_window_size"> <TextView android:id="@+id/add" android:layout_width="18dp" android:layout_height="20dp" android:gravity="right|bottom" android:paddingRight="2dp" android:text="9" android:textSize="10sp" android:textColor="#000000" android:textStyle="bold" /> </FrameLayout> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_type" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/ll_search" /> <View android:id="@+id/view" android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="4dp" android:background="@color/color_D7CCC1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/rv_type" /> <FrameLayout android:id="@+id/fl_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/view" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>```必须要创建列表项布局文件item_suggestion.xml吗?
最新发布
03-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值