0

这是我的代码:

   <LinearLayout
   android:layout_width="200dp"
   android:layout_height="400dp"
   android:background="#D2B48C"
   android:orientation="vertical">

   <TextView
   android:text="sahil"
   android:layout_width="wrap_content"
   android:layout_height="0dp"
   android:layout_weight="6"
   android:background="#DC143C"/>

   <TextView
   android:text="sahil"
   android:layout_width="wrap_content"
   android:layout_height="0dp"
   android:layout_weight="1"
   android:background="#DC143C"/>

   </LinearLayout>

即使我将文本视图的布局宽度设置为“wrap_content”,它仍然覆盖了为线性布局设置的整个空间,即 200dp

4

1 回答 1

0

如果你想使用权重来处理你的 UI,那么你应该将宽度或高度设置为 0。否则,它将不起作用。

<LinearLayout
            android:layout_width="500dp"
            android:layout_height="20dp" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="@android:color/holo_green_light"
                android:gravity="center"
                android:text="30%"
                android:textColor="@android:color/white" >
            </TextView>

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:background="@android:color/holo_blue_bright"
                android:gravity="center"
                android:text="20%"
                android:textColor="@android:color/white" >
            </TextView>

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="5"
                android:background="@android:color/holo_orange_dark"
                android:gravity="center"
                android:text="50%"
                android:textColor="@android:color/white" >
            </TextView>
 </LinearLayout>

它看起来像: 在此处输入图像描述

于 2020-06-15T06:46:36.453 回答