In android layouts what is the effect/meaning of layout_height=“0dip”

前端 未结 4 836
半阙折子戏
半阙折子戏 2020-12-01 10:01

I\'ve seen several examples that use android:layout_height=\"0px\" or \"0dip\" but i do not understand the impact of this. It seems that would make the layout 0 pixels tall.

相关标签:
4条回答
  • 2020-12-01 10:51

    Yep you are right about the weight, when you want the width or height to be controlled by weight its convention to set that value to 0dip and let the weight control the actual value. Although I am pretty sure 0 is just arbitrary here you could put anything but putting 0 makes your intention more clear.

    0 讨论(0)
  • 2020-12-01 10:54

    When we have to assign equal weight to the different view in LINEAR LAYOUT then we assign either layout/width = 0dp(for horizontal orientation) or layout/height = 0dp(for vertical orientation) and set View /weight ==1 for every view inside that Linear Layout.

    Advantage:::: -- on assigning width or height to 0dp then it have no impact and due to weight==1 all the view occupies same space and covered the whole screen size.

    0 讨论(0)
  • 2020-12-01 11:01

    When using a LinearLayout if you set the layout_weight to a non-zero value and set the layout_height (or layout_width) to 0px or 0dip then the LinearLayout distributes any unassigned space along the appropriate axis based on the weights. So for example, if you look at the layout below the View with id *gestures_overlay* it has layout_height 0dip and layout_weight 1 so the parent LinearLayout stretches it to fill the available vertical space between the 2 surrounding LinearLayouts. If there was another View with the same 0dip layout_height and a layout_weight value then they would share the vertical space based on their weight values.

    <?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>
    
    0 讨论(0)
  • 2020-12-01 11:03

    A relevant example from the official developer docs (http://developer.android.com/guide/topics/ui/layout/linear.html):

    Equally weighted children

    To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".

    0 讨论(0)
提交回复
热议问题