Linear Layout and weight in Android

后端 未结 18 2362
伪装坚强ぢ
伪装坚强ぢ 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 03:00

    Try setting the layout_width of both buttons to "0dip" and the weight of both buttons to 0.5

    0 讨论(0)
  • 2020-11-22 03:00

    This is perfect answer of your problem

      <LinearLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:orientation="horizontal"  >   
         <Button 
            android:text="Register" android:id="@+id/register"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:padding="10dip" weight="1" />
         <Button 
            android:text="Not this time" android:id="@+id/cancel"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:padding="10dip" weight="1" />
      </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 03:03

    LinearLayout supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Default weight is zero

    calculation to assign any Remaining/Extra space between child. (not the total space)

    space assign to child = (child individual weight) / (sum of weight of every child in Linear Layout)

    Example (1): if there are three text boxes and two of them declare a weight of 1, while the third one is given no weight (0), then Remaining/Extra space assign to

    1st text box = 1/(1+1+0) 
    2nd text box = 1/(1+1+0) 
    3rd text box = 0/(1+1+0) 
    

    Example (2) : let's say we have a text label and two text edit elements in a horizontal row. The label has no layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1, the remaining width in the parent layout will be split equally between them (because we claim they are equally important).

    calculation : 
    1st label = 0/(0+1+1) 
    2nd text box = 1/(0+1+1) 
    3rd text box = 1/(0+1+1)
    

    If the first one text box has a layout_weight of 1 and the second text box has a layout_weight of 2, then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).

    calculation : 
    1st label = 0/(0+1+2) 
    2nd text box = 1/(0+1+2) 
    3rd text box = 2/(0+1+2) 
    
    0 讨论(0)
  • 2020-11-22 03:06
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/logonFormButtons"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:baselineAligned="true"       
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/logonFormBTLogon"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"            
                android:text="@string/logon"
                android:layout_weight="0.5" />
    
            <Button
                android:id="@+id/logonFormBTCancel"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"            
                android:text="@string/cancel"
                android:layout_weight="0.5" />
        </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 03:07

    You have to write like this its Working for me

    <LinearLayout
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal"
                android:weightSum="2">
    
             <Button
                android:text="Register"
                android:id="@+id/register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dip"
                android:layout_weight="1" />
    
             <Button
                android:text="Not this time"
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dip"
                android:layout_weight="1" />
    
    0 讨论(0)
  • 2020-11-22 03:10
     <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="#008">
    
                <RelativeLayout
                    android:id="@+id/paneltamrin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
    
                    >
                    <Button
                        android:id="@+id/BtnT1"
                        android:layout_width="wrap_content"
                        android:layout_height="150dp"
                        android:drawableTop="@android:drawable/ic_menu_edit"
                        android:drawablePadding="6dp"
                        android:padding="15dp"
                        android:text="AndroidDhina"
                        android:textColor="#000"
                        android:textStyle="bold" />
                </RelativeLayout>
    
                <RelativeLayout
                    android:id="@+id/paneltamrin2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    >
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="150dp"
                         android:drawableTop="@android:drawable/ic_menu_edit"
                        android:drawablePadding="6dp"
                        android:padding="15dp"
                        android:text="AndroidDhina"
                        android:textColor="#000"
                        android:textStyle="bold" />
    
                </RelativeLayout>
            </LinearLayout>
    

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