繁体   English   中英

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

[英]Nested layout_weight bad for performance: Why do I get Lint warning when I set layout_width value to 0dp?

我想要一个SearchViewActivity的布局的顶部,然后在其下方,我需要添加两个彼此水平对齐的fragment s,以使右边的fragment比左边的fragment占据两倍的水平空间。

所以我的意思是:

包含两个片段的ViewGroup在绘制SearchView之后应占据所有剩余空间。 所以我给它的layout_weight1 然后,我为两个fragmnets添加了LinearLayout和两个FrameLayout ,并将左侧的Layout_weight1 ,右侧的设置为2

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

我在SO上阅读了有关内容, 发现了这一点

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

此处有关开发人员培训的基础教程以这种方式说明了这一点:

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

但是,因为在我的布局,我设置layout_width="0dp"当父母LinearLayoutorientationvertical ,而layout_height="0dp"当父母LinearLayoutorientationhorizontal 为什么我仍然收到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>

这是一个警告,在这种情况下,您应该没事,这还可以防止您滥用大量嵌套的权重并拖延您的应用程序。 另一个选项是在运行时获取屏幕尺寸并手动设置“帧布局”的宽度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM