Linear Layout and weight in Android

后端 未结 18 2482
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 02:41

I always read about this funny weight value in the Android documentations. Now I want to try it for the first time but it isn\'t working at all.

As I understand it

相关标签:
18条回答
  • 2020-11-22 02:44

    Like answer of @Manoj Seelan

    Replace android:layout_weight With android:weight.

    When you use Weight with LinearLayout. you must add weightSum in LinearLayout and according to orientation of your LinearLayout you must setting 0dp for Width/Height to all LinearLayout`s Children views

    Example :

    If The orientation of Linearlayout is Vertical , then Set Width of all LinearLayout`s Children views with 0dp

     <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical"
         android:weightSum="3">
    
         <Button
            android:text="Register"
            android:id="@+id/register"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_weight="2" />
    
         <Button
            android:text="Not this time"
            android:id="@+id/cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_weight="1" />
    
      </LinearLayout>
    

    If the orientation Linearlayout of is horizontal , then Set Height of all LinearLayout`s Children views with 0dp.

     <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:weightSum="3">
    
         <Button
            android:text="Register"
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:padding="10dip"
            android:layout_weight="2" />
    
         <Button
            android:text="Not this time"
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:padding="10dip"
            android:layout_weight="1" />
    
      </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 02:44

    Plus you need to add this android:layout_width="0dp" for children views [Button views] of LinerLayout

    0 讨论(0)
  • 2020-11-22 02:45

    It's android:layout_weight. Weight can only be used in LinearLayout. If the orientation of linearlayout is Vertical, then use android:layout_height="0dp" and if the orientation is horizontal, then use android:layout_width = "0dp". It'll work perfectly.

    0 讨论(0)
  • 2020-11-22 02:45

    In the above XML, set the android:layout_weight of the linear layout as 2: android:layout_weight="2"

    0 讨论(0)
  • 2020-11-22 02:47

    3 things to remember:

    • set the android:layout_width of the children to "0dp"
    • set the android:weightSum of the parent (edit: as Jason Moore noticed, this attribute is optional, because by default it is set to the children's layout_weight sum)
    • set the android:layout_weight of each child proportionally (e.g. weightSum="5", three children: layout_weight="1", layout_weight="3", layout_weight="1")

    Example:

        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="5">
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="2" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
    
        </LinearLayout>
    

    And the result:

    Layout weight example

    0 讨论(0)
  • 2020-11-22 02:48

    You are not setting the layout_weight property. Your code reads weight="1" and it should read android:layout_weight="1".

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