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

前端 未结 9 727
梦毁少年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: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.

    
        android:orientation="horizontal"
        android:layout_gravity="center">
    
       
      .....
    

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

提交回复
热议问题