为什么0dp被认为是性能增强?

Kir*_*irk 73 android android-layout android-lint

这个问题末尾的答案已经填写完整,结合了评论和解决方案.

我四处搜索,但没有发现任何真正解释为什么Android Lint以及一些Eclipse提示建议用一些layout_heightlayout_width值替换0dp.

例如,我有一个ListView建议改变

之前

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>
Run Code Online (Sandbox Code Playgroud)

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
</ListView>
Run Code Online (Sandbox Code Playgroud)

同样,它建议更改ListView项.这些在变化之前和之后看起来都一样,但我有兴趣理解为什么这些是性能提升器.

任何人都有解释原因?如果它有帮助,这里是一般布局ListView.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/logo_splash"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ImageView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@color/background"
        android:layout_below="@id/logo_splash">

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </ListView>

        <TextView
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no_upcoming" />

    </LinearLayout>        
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

回答

我在这里给出答案,因为它实际上是答案和下面引用的链接的组合.如果我对某事有误,请告诉我.

来自0dip layout_height或layouth_width的诀窍是什么?

有3种常规布局属性可用于宽度高度

  1. android:layout_height
  2. android:layout_width
  3. android:layout_weight

当a LinearLayout垂直时,layout_weight则会影响子s()的高度.设置为将导致忽略此属性.ViewListViewlayout_height0dp

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </ListView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

当a LinearLayout水平的时,layout_weight则会影响子s()的宽度.设置为将导致忽略此属性.ViewListViewlayout_width0dp

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">
    <ListView
        android:id="@android:id/list"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </ListView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

想要忽略该属性的原因是,如果您不忽略它,它将用于计算使用更多CPU时间的布局.

此外,这可以防止在使用三个属性的组合时对布局应该是什么样的混淆.@android开发人员在下面的答案中强调了这一点.

此外,Android LintEclipse都说要使用0dip.从下面这个问题的答案,你可以使用0dip,0dp,0px等自零大小是任何单位的相同.

避免在ListView上使用wrap_content

来自ListView的Layout_width

如果你曾经想过为什么getView(...)像我一样被召唤这么多次,那么事实证明它与之相关wrap_content.

wrap_content像我上面使用的那样使用将导致所有的孩子View被测量,这将导致进一步的CPU时间.此测量将导致您getView(...)被调用.我现在测试了这个,并且getView(...)调用的次数大大减少了.

当我wrap_content在两个ListViews 上使用时getView(...),每行一次被调用3次,而另一行每次被调用ListView4次.

将此更改为建议0dp,getView(...)每行只调用一次.这是一个相当大的改进,但更多的是避免wrap_contentListView它而不是它0dp.

然而,0dp由于这个原因,该建议确实大大提高了性能.

Lal*_*ani 23

首先,你有这个,

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</ListView>
Run Code Online (Sandbox Code Playgroud)

永远不要将ListView的高度作为wrap_content,这将导致麻烦.Here是的原因和this answer.

更进一步,

我四处搜索,但没有发现任何真正解释为什么Android Lint以及一些Eclipse提示建议用0dp替换一些layout_height和layout_width值.

因为你正在使用layout_weight = "1"它意味着你的ListView具有尽可能多的高度.因此,在这种情况下,不需要layout_height = "wrap_content"仅使用它来改变它,android:layout_height="0dp"并且ListView的高度将由其管理layout_weight = "1".


Arc*_*pgc 12

因此,当在视图X上使用android:layout_weight并且LinearLayout是水平的时,则简单地忽略X的android:layout_width.

类似地,当在View X上使用android:layout_weight并且LinearLayout是垂直的时,则忽略X的android:layout_height.

这实际上意味着,您可以在这些被忽略的字段中放置任何内容:0dp或fill_parent或wrap_content.没关系.但是建议使用0dp,因此View不会对其高度或宽度进行额外的计算(然后会被忽略).这个小技巧简单地节省了CPU周期.

来自:

0dip layout_height或layouth_width的诀窍是什么?


and*_*per 5

据我所知,使用0dp(或0px btw,它是相同的,因为0是0,无论这里是什么单位)和wrap_content或fill_parent(或match_parent,它是相同的)之间存在差异.

这取决于你使用的重量.如果你只使用1的重量,它们看起来都一样,但意义总是不同的,这对性能很重要.

为了显示这一点,请尝试以下方法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/textView1" android:layout_width="match_parent"
    android:layout_height="0px" android:text="1" android:background="#ffff0000"
    android:layout_weight="1" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

  <TextView android:id="@+id/textView2" android:layout_width="match_parent"
    android:layout_height="0px" android:text="2" android:background="#ff00ff00"
    android:layout_weight="2" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

  <TextView android:id="@+id/textView3" android:layout_width="match_parent"
    android:layout_height="0px" android:text="3" android:background="#ff0000ff"
    android:layout_weight="3" android:gravity="center"
    android:textColor="#ffffffff" android:textSize="20sp" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后尝试用match_parent替换0px.你会看到结果非常不同.

通常,为了更好地理解和更好的性能,你会想要使用0px.