Linear Layout and weight in Android

后端 未结 18 2363
伪装坚强ぢ
伪装坚强ぢ 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:49

    This image summarizes the Linear layout.

    You can follow this link for more information on the topic. Just Maths - Views, View Groups and Layouts

    Video Tutorial For Linear Layout : Width, Height & Weights

    Android Linear Layout Tutorial

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

    Perhaps setting both of the buttons layout_width properties to "fill_parent" will do the trick.

    I just tested this code and it works in the emulator:

    <LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content">
    
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="hello world"/>
    
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="goodbye world"/>
    
    </LinearLayout>
    

    Be sure to set layout_width to "fill_parent" on both buttons.

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

    Substitute wrap_content with fill_parent.

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

    In the width field of button, replace wrap-content with 0dp.
    Use layout_weight attribute of a view.

    android:layout_width="0dp"  
    

    This is how your code will look like:

    <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
    
     <Button
        android:text="Register"
        android:id="@+id/register"
        android:layout_width="0dp"
        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="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="1" />    
    
    </LinearLayout>
    

    layout_weight is used to distribute the whatever left space into proportions. In this case, the two buttons are taking "0dp" width. So, the remaining space will be divided into 1:1 proportion among them, i.e. the space will be divided equally between the Button Views.

    0 讨论(0)
  • 2020-11-22 02:56
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Button 1" />
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Button 2" />
    
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Button 3" />
    
        </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 02:58

    Below are the changes (Marked in BOLD) in your code:

    <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">
    
         <Button
            android:text="Register"
            android:id="@+id/register"
            android:layout_width="0dp" //changes made here
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_weight="1" /> //changes made here
    
         <Button
            android:text="Not this time"
            android:id="@+id/cancel"
            android:layout_width="0dp" //changes made here
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:layout_weight="1" /> //changes made here
    
      </LinearLayout>
    

    Since your LinearLayout has orientation as horizontal, therefore you will need to keep your width only as 0dp. for using weights in that direction . (If your orientation was vertical, you would have kept your height only 0dp).

    Since there are 2 views and you have placed android:layout_weight="1" for both the views, it means it will divide the two views equally in horizontal direction (or by width).

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