What is android:weightSum in android, and how does it work?

前端 未结 9 729
梦毁少年i
梦毁少年i 2020-11-22 04:02

I want to know: What is android:weightSum and layout weight, and how do they work?

相关标签:
9条回答
  • 2020-11-22 04:20

    If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0. Must be a floating point value, such as "1.2"

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/main_rel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="2.0" >
    
            <RelativeLayout
                android:id="@+id/child_one"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.0"
                android:background="#0000FF" >
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/child_two"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.0"
                android:background="#00FF00" >
            </RelativeLayout>
    
        </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 04:23

    Layout Weight works like a ratio. For example, if there is a vertical layout and there are two items(such as buttons or textviews), one having layout weight 2 and the other having layout weight 3 respectively. Then the 1st item will occupy 2 out of 5 portion of the screen/layout and the other one 3 out of 5 portion. Here 5 is the weight sum. i.e. Weight sum divides the whole layout into defined portions. And Layout Weight defines how much portion does the particular item occupies out of the total Weight Sum pre-defined. Weight sum can be manually declared as well. Buttons, textviews, edittexts etc all are organized using weightsum and layout weight when using linear layouts for UI design.

    0 讨论(0)
  • 2020-11-22 04:24

    Per documentation, android:weightSum defines the maximum weight sum, and is calculated as the sum of the layout_weight of all the children if not specified explicitly.

    Let's consider an example with a LinearLayout with horizontal orientation and 3 ImageViews inside it. Now we want these ImageViews always to take equal space. To acheive this, you can set the layout_weight of each ImageView to 1 and the weightSum will be calculated to be equal to 3 as shown in the comment.

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        <!-- android:weightSum="3" -->
        android:orientation="horizontal"
        android:layout_gravity="center">
    
       <ImageView
           android:layout_height="wrap_content"       
           android:layout_weight="1"
           android:layout_width="0dp"/>
      .....
    

    weightSum is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.

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