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

Luk*_*kap 17 height layout android

我的意思是为什么有人希望他们认为是0dip高度?我已经看过很多次,必须有某种技巧,但我不明白.

        <TextView android:gravity="top" android:textColor="#FFFF0000"
            android:textSize="20dip" android:text="TextView"
            android:layout_height="0dip" android:layout_width="fill_parent"
            android:id="@+id/contactName"></TextView>
Run Code Online (Sandbox Code Playgroud)

他们为什么不使用例如wrap_content?他们想要实现什么?

ina*_*ruk 25

这大量用于视图LinearLayout.有三个"布局"属性LinearLayout可以识别:

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

您可以android:layout_weight教程项目中找到示例.

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

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

这实际上意味着,你可以把任何东西在那些忽略的字段:0dpfill_parentwrap_content.没关系.但是建议使用0dp这样View的,不要额外计算它们的高度或宽度(然后忽略它).这个小技巧简单地节省了CPU周期.

  • 这是不正确的.尝试使用不同的权重(例如1,2,3)并看到使用match_parent和0px(或0dp)之间存在差异 (5认同)

Dim*_*ris 18

这通常用于在linearlayout中有很多视图并且设置android:layout_weight ="1"以便两个视图占用相等的空间.例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="TextView" />

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

在这种情况下,视图将采用与所有其他视图一样多的高度.