在下面的代码中,Android studio提示
Layout weights require a widget to be measured twice. When a LinearLayout
with non-zero weights is nested inside another LinearLayout with non-zero
weights ,then the number of measurements increases exponently.
这要怎么改?
//这段是从一段里截取的
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="@string/again"
android:id="@+id/again"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="@string/confirm"
android:id="@+id/confirm"
android:layout_weight="1" />
</LinearLayout>
你就一个LinearLayout,你怎么用weight属性呢,两个button有一个父LinearLayout,此LinearLayout有宽度故可以用weight。如果你的LinearLayout也有父节点,就可以用weight。
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!--设置高度,去掉weight-->
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="@string/again"
android:id="@+id/again"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="@string/confirm"
android:id="@+id/confirm"
android:layout_weight="1" />
</LinearLayout> <LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="@string/again"
android:id="@+id/again"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="@string/confirm"
android:id="@+id/confirm"
android:layout_weight="1" />
</LinearLayout>
不好意思代码多复制了一段,看上边的
顶层layout可以不用,里面的button要平分就是用weight一样的值
这是Android-Lint功能,报错是因为最外层layout_weight无意义,影响性能。所以提示错误
eclipse可以http://blog.csdn.net/lidhsr/article/details/38435733关闭,
可以看看studio http://blog.sina.com.cn/s/blog_4e1e357d0102wbjc.html里面是不是也有设置
你这一段设置的android:orientation="horizontal"是水平布局,但是你设置android:layout_width="fill_parent"
android:layout_height="0dp"这样就和水平布局冲突了,应该设置为android:layout_width="0dp"
android:layout_height="fill_parent"
父子都用了weight 测量时会需要很久
这个其实不算错误,只是提醒你这样嵌套 layout weight 属性,会使控件多次测量宽高,不提倡而已。
顶层layout最好不用,
里面的两个button设置weight一样的值,就均分父控件的大小了。