简体   繁体   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?

I want a SearchView at the top of an Activity 's layout, and then below that I need to add two fragment s aligned horizontally next to each other, such that the right one take double the horizontal space than the left one. 我想要一个SearchViewActivity的布局的顶部,然后在其下方,我需要添加两个彼此水平对齐的fragment s,以使右边的fragment比左边的fragment占据两倍的水平空间。

So what I mean is: 所以我的意思是:

The ViewGroup containing both the fragments should take up all the remaining space after the SearchView is drawn. 包含两个片段的ViewGroup在绘制SearchView之后应占据所有剩余空间。 So I gave it a layout_weight of 1 . 所以我给它的layout_weight1 Then I added a LinearLayout with two FrameLayout s for the fragmnets, and gave the left one a Layout_weight of 1 , and the right one the value of 2 . 然后,我为两个fragmnets添加了LinearLayout和两个FrameLayout ,并将左侧的Layout_weight1 ,右侧的设置为2

But I get the Lint warning that nested weights are bad for performance. 但是我得到了Lint警告,嵌套权重不利于性能。

I read about it on SO and found this : 我在SO上阅读了有关内容, 发现了这一点

Layout weights require a widget to be measured twice. 布局权重要求对小部件进行两次测量。

This basic tutorial on the developer training here says this thing this way: 此处有关开发人员培训的基础教程以这种方式说明了这一点:

To improve the layout efficiency when you specify the weight, you should change the width of the EditText to be zero (0dp). 为了在指定权重时提高布局效率,应将EditText的宽度更改为零(0dp)。 Setting the width to zero improves layout performance because using "wrap_content" as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space. 将宽度设置为零可提高布局性能,因为使用“ wrap_content”作为宽度需要系统计算最终不相关的宽度,因为权重值需要另外计算宽度以填充剩余空间。

But since in my layout I am setting the layout_width="0dp" when the parent LinearLayout 's orientation is vertical , and layout_height="0dp" when the parent LinearLayout 's orientation is horizontal . 但是,因为在我的布局,我设置layout_width="0dp"当父母LinearLayoutorientationvertical ,而layout_height="0dp"当父母LinearLayoutorientationhorizontal Why am I still getting the Lint warning? 为什么我仍然收到Lint警告?

What can I do about this? 我该怎么办? Because RelativeLayout can't specify how much of the "remaining space" is an element going to take (specified by layout_weight , which does not seem to be included in RelativeLayout.LayoutParams ). 因为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>

It's a warning, in this case you should be OK, it's more to keep you from abusing a lot of nested weights and bogging down your app. 这是一个警告,在这种情况下,您应该没事,这还可以防止您滥用大量嵌套的权重并拖延您的应用程序。 The other option is to get the screen size at run time and set the width of the Frame Layouts manually. 另一个选项是在运行时获取屏幕尺寸并手动设置“帧布局”的宽度。

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

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