理解xml中的wrap_content函数时出现问题 [英] problem in understanding wrap_content function in xml

查看:50
本文介绍了理解xml中的wrap_content函数时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

   <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

even though I have set the layout width for text view to be "wrap_content", it still covers the whole space set for linear layout which is 200dp

推荐答案

如果要使用权重处理UI,则应将width或height设置为0.否则将无法正常工作.

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>

它看起来像:

这篇关于理解xml中的wrap_content函数时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆