在android布局中,layout_height ="0dip"的效果/含义是什么?

New*_*Guy 38 android android-layout

我见过几个使用android的例子:layout_height ="0px"或"0dip",但我不明白这个的影响.似乎这将使布局高0像素.是否减轻了价值,但还有一些其他因素,如"权重"或任何父视图的高度?

Nat*_*ann 35

你是否正确的权重,当你想要按重量控制宽度或高度时,将该值设置为0dip并让重量控制实际值.虽然我很确定0在这里是任意的,你可以放任何东西,但是把0放在一起会让你的意图更清晰.

  • 将`layout_width`或`layout_height`设置为`0px`或`0dip`对于运行时性能而不是约定更为重要.将其设置为"0px"(或"0dip")将跳过初始初始化所需的cpu周期,并根据需要调整视图的宽度/高度.总之,运行时只需计算一次高度/宽度而不是2次...... (30认同)

Fem*_*emi 11

如果将LinearLayout设置为layout_weight非零值并将layout_height(或layout_width)设置为0px或0dip,则LinearLayout会根据权重沿适当的轴分配任何未分配的空间.因此,例如,如果您查看带有id*layout_heightgestures_overlay*的View下面的布局,它有0dip和layout_weight1,因此父LinearLayout会拉伸它以填充2个周围LinearLayouts之间的可用垂直空间.如果存在具有相同0dip layout_heightlayout_weight值的另一个视图,则它们将基于它们的权重值共享垂直空间.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="6dip"

            android:text="@string/prompt_gesture_name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/gesture_name"
            android:layout_width="0dip"
            android:layout_weight="1.0"
            android:layout_height="wrap_content"

            android:maxLength="40"
            android:singleLine="true" />

    </LinearLayout>

    <android.gesture.GestureOverlayView
        android:id="@+id/gestures_overlay"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0"

        android:gestureStrokeType="multiple" />

    <LinearLayout
        style="@android:style/ButtonBar"

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <Button
            android:id="@+id/done"

            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            android:enabled="false"

            android:onClick="addGesture"
            android:text="@string/button_done" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            android:onClick="cancelGesture"
            android:text="@string/button_discard" />

    </LinearLayout>

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