weightsum背后的Android逻辑

weightsum背后的Android逻辑,android,Android,因此,我有以下XML代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayoutOuter" android:orientation="vertical" android:layout_width="match_parent" android:la

因此,我有以下XML代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayoutOuter"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3.0"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1.0"
>
<Gallery
 android:id="@+id/galleryMain"
 android:layout_width="match_parent"
 android:layout_height="90dp">
</Gallery>
<LinearLayout
 android:id="@+id/linearLayoutInner"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@layout/gallery_image_background"
/>
</LinearLayout>


<TextView
android:id="@+id/galleryTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.0"
>
</TextView>

</LinearLayout>

目前它的尺寸正确,但我不认为这是正确的

从3.0可能的权重中获得1.0的线性布局需要大约2/3的空间。 从3.0可能的wight中获得2.0的TextView占用了大约1/3的空间

这真的意味着什么?它的大小和我想要的一样,但是。。。我不太明白背后的逻辑

这真的意味着什么

考虑到你写它的方式,是的,但这就是为什么我们通常不这样写的原因。:-)

更容易理解的权重和
android:weightSum
方法是将高度设置为
0dp
,而不是
match\u parent
。然后,每个孩子根据权重获得可用空间的一部分

因此,要以这种方式分割2/3和1/3,您需要:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayoutOuter"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3.0"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="2.0"
>
<Gallery
 android:id="@+id/galleryMain"
 android:layout_width="match_parent"
 android:layout_height="90dp">
</Gallery>
<LinearLayout
 android:id="@+id/linearLayoutInner"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@layout/gallery_image_background"
/>
</LinearLayout>


<TextView
android:id="@+id/galleryTextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
>
</TextView>

</LinearLayout>

请注意:

  • 这里可以用整数

  • 在这种情况下,您不需要
    android:weightSum
    ,因为您的权重之和已经是该值。如果权重之和小于实际总数,则可以使用
    android:weightSum
    ,这表明部分空间应保留为空白,并按空白处理(默认情况下,出现在
    线性布局的子项之后)

这真的意味着什么

考虑到你写它的方式,是的,但这就是为什么我们通常不这样写的原因。:-)

更容易理解的权重和
android:weightSum
方法是将高度设置为
0dp
,而不是
match\u parent
。然后,每个孩子根据权重获得可用空间的一部分

因此,要以这种方式分割2/3和1/3,您需要:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayoutOuter"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3.0"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="2.0"
>
<Gallery
 android:id="@+id/galleryMain"
 android:layout_width="match_parent"
 android:layout_height="90dp">
</Gallery>
<LinearLayout
 android:id="@+id/linearLayoutInner"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@layout/gallery_image_background"
/>
</LinearLayout>


<TextView
android:id="@+id/galleryTextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
>
</TextView>

</LinearLayout>

请注意:

  • 这里可以用整数

  • 在这种情况下,您不需要
    android:weightSum
    ,因为您的权重之和已经是该值。如果权重之和小于实际总数,则可以使用
    android:weightSum
    ,这表明部分空间应保留为空白,并按空白处理(默认情况下,出现在
    线性布局的子项之后)


尝试将权重值转换为整数类型,而不是将其设为1.0,2.0设为1,2使用
0dp
根据您希望两个视图拉伸的方向对宽度或高度进行设置。使用0dp似乎有效:)尝试将权重值转换为整数类型,而不是设为1.0,2.0设为1,2根据您希望两个视图拉伸的方向,在宽度或高度上使用
0dp
。使用0dp似乎有效:)谢谢!这很有效。我仍然有点困惑,如果所有共享高度的控件都设置为“match_parent”,为什么“match_parent”不起作用。但是哦,好吧,现在我知道如何正确地做了:)谢谢!这很有效。我仍然有点困惑,如果所有共享高度的控件都设置为“match_parent”,为什么“match_parent”不起作用。但是哦,好吧,现在我知道如何正确地做了:)