Android是什么:weightSum在Android中,它是如何工作的?它是、工作、Android、weightSum

2023-09-11 11:19:34 作者:镜中嘻

我想知道:什么是机器人:weightSum和布局的重量,以及他们是如何工作的。

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

推荐答案

最好是用一个例子来解释。

It is better to explain with an example.

您有一个的LinearLayout 与水平方向,你有3 ImageViews 里面,你想这些 ImageViews 总是采取平等的空间。

You have a LinearLayout with horizontal orientation and you have 3 ImageViews inside it and you want these ImageViews always to take equal space.

  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="3"
    android:orientation="horizontal"
    android:layout_gravity="center">

您可以设置 layout_weight 每个的ImageView 1和 weightSum 在的LinearLayout 3实现这一目标。

You can set the layout_weight of each ImageView to 1 and the weightSum in LinearLayout to 3 to achieve this.

<ImageView
    android:layout_height="wrap_content"       
    android:layout_weight="1"
    android:layout_width="0dp"/>

好一点的是,这将正常工作的任何设备,如果你直接设置宽度和高度,这将不会发生。

The good point is that this will work correctly for any device, which will not happen if you set width and height directly.