嵌套layout_weight对性能不利:当我将layout_width值设置为0dp时,为什么会出现Lint警告?

时间:2014-12-17 17:06:49

标签: android android-layout android-linearlayout android-relativelayout android-layout-weight

我想在SearchView的布局顶部放置一个Activity,然后在下面我需要添加两个水平对齐的fragment,这样右边的水平空间比左边的水平空间加倍。

所以我的意思是:

包含两个片段的ViewGroup应该在绘制SearchView后占用所有剩余空间。所以我给了它layout_weight 1。 然后我添加了一个LinearLayout,其中两个FrameLayout用于fragmnets,左边一个Layout_weight1,右边一个为2 }。

但我得到Lint警告,嵌套权重对性能不利。

我在SO和found this上阅读了它:

  

布局权重需要对窗口小部件进行两次测量。

basic tutorial on the developer training here就是这样说的:

  

为了在指定重量时提高布局效率,您   应该将EditText的宽度更改为零(0dp)。设置   宽度为零可以改善布局性能,因为使用" wrap_content"   因为宽度要求系统计算宽度   最终无关紧要,因为重量值需要另一个宽度   计算以填补剩余空间。

但是,在我的布局中,当父layout_width="0dp" LinearLayout orientationvertical时,我设置layout_height="0dp",而LinearLayout时为orientationhorizontal&#39; RelativeLayout<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <android.support.v7.widget.SearchView android:id="@+id/searchActivity_searchView" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:layout_weight="1" android:layout_height="0dp" android:layout_width="match_parent" > <FrameLayout android:id="@+id/searchActivity_frameLayout" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /><!-- Nested weights are bad for performance Lint Warning --> <FrameLayout android:id="@+id/searchActivity_frameLayout1" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout> 。为什么我仍然得到Lint警告?

我该怎么办?因为{{1}}无法指定剩余空间的多少&#34;是一个要采用的元素(由layout_weight指定,似乎不包含在RelativeLayout.LayoutParams中)。

{{1}}

1 个答案:

答案 0 :(得分:1)

这是一个警告,在这种情况下你应该没问题,更多的是让你不要滥用很多嵌套的权重并让你的应用程序陷入困境。另一个选项是在运行时获取屏幕大小并手动设置帧布局的宽度。