【问题标题】:Nested layout_weight bad for performance: Why do I get Lint warning when I set layout_width value to 0dp?嵌套的 layout_weight 对性能不利:为什么当我将 layout_width 值设置为 0dp 时会收到 Lint 警告?
【发布时间】:2014-12-17 17:06:48
【问题描述】:

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

所以我的意思是:

包含两个片段的ViewGroup 应在绘制SearchView 后占用所有剩余空间。所以我给了它一个layout_weight1。 然后我为碎片网添加了一个LinearLayout和两个FrameLayouts,并给左边一个1Layout_weight,右边一个2的值。

但是我收到了 Lint 警告说嵌套权重不利于性能。

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

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

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

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

但是由于在我的布局中,当父 LinearLayoutorientationvertical 时,我设置了 layout_width="0dp",而当父 LinearLayoutorientationhorizontal 时,layout_height="0dp" .为什么我仍然收到 Lint 警告?

对此我能做些什么?因为RelativeLayout 无法指定元素要占用多少“剩余空间”(由layout_weight 指定,RelativeLayout.LayoutParams 中似乎没有包含)。

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

【问题讨论】:

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


    【解决方案1】:

    这是一个警告,在这种情况下你应该没问题,更多的是防止你滥用大量嵌套权重并让你的应用程序陷入困境。另一种选择是在运行时获取屏幕尺寸并手动设置框架布局的宽度。

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 2012-08-10
      相关资源
      最近更新 更多