如何消除这个警告?
使用0dip的layout_height代替wrap_content可以提高性能。
如何消除这个警告?
使用0dip的layout_height代替wrap_content可以提高性能。
好的,让我通过一个例子来解释一下:
<?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="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello World!!" />
</LinearLayout>
在上面的例子中,我使用了android:layout_weight="1"
来为TextView指定布局权重,告诉布局我的TextView将占用与父布局(LinearLayout)相同的高度。因此,在这种情况下,android:layout_height="wrap_content"
是没有用的,因为TextView
将具有父LinearLayout
的完整大小。所以,在这种情况下,最好添加android:layout_height="0dp"
,使TextView自动调整大小以适应父布局的高度。
如果您为layout_weight
指定了任何值,即使它被设置为0也会引发此警告。
问题在于,在将其设置为0dp的情况下,高度/宽度为0,结果元素根本不会显示。只需删除layout_weight
参数,一切都很好。
当然,正如许多其他人所说,如果您指定了大于零的layout_weight
,则这不是一个问题。只需将layout_height
或layout_width
(取决于布局)设置为0dp即可。
在垂直父布局中,您必须在子元素中使用layout_weight
属性。因此,在这种情况下,您已经告诉父元素要应用多少高度到子元素。
因此,在子元素中替换
android:layout_height="wrap_content"
使用
android:layout_height="0dp"
然而,这只是一个猜测,你应该发布你的整个布局XML。