problem in understanding wrap_content function in xml

后端 未结 2 545
不知归路
不知归路 2021-01-26 07:59

this is my code:

   

        
相关标签:
2条回答
  • 2021-01-26 08:37

    If you want to handle your UI using weight then you should set width or height to 0. Otherwise, it will not work.

    <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>
    

    It will look like:

    0 讨论(0)
  • 2021-01-26 08:50

    That is because you are providing weight . Remove weight and run it again . Weight let:s the textview occupy all the leftover space.

    0 讨论(0)
提交回复
热议问题