Android LinearLayout layout_weight 行为

Android LinearLayout layout_weight behavior

我需要安排 4 列视图,其中第一列占整个高度。其他 3 个包含 2 个垂直放置的视图。我想到了使用由第一个视图和 3 个嵌套的垂直 LinearLayouts 组成的父级水平 LinearLayout。

现在我需要将最后两列的大小设为前两列的一半。所以我使用 layout_weight 并将它的后两个设置为 1,前两个设置为 2。然而,这会导致最后两列占据整个 space.

我想这一定与其余的布局设置有关,但由于我没有经验,我不能完全理解它。希望你们中的一个能帮助我。

这是xml:

<LinearLayout
    android:layout_width="400dp"
    android:layout_height="100dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toTopOf="@id/table_playfield"
    app:layout_constraintTop_toBottomOf="@id/table_tile2"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" >

    <com.example.se2_gruppenphase_ss21.game.TimerView
        android:id="@+id/timerView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="2" >

        <Button
            android:id="@+id/remove"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/remove_tile"
            app:backgroundTint="#2196F3" />

        <Button
            android:id="@+id/ubongo_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:fontFamily="@font/architects_daughter"
            android:text="UBONGO !!"
            android:textSize="16sp"
            android:textStyle="bold"
            app:backgroundTint="#FF1010" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1" >

        <Button
            android:id="@+id/mirror_vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/mirror_vertical"
            app:backgroundTint="#3F51B5" />

        <Button
            android:id="@+id/rotate_left"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/rotate_left"
            app:backgroundTint="#3F51B5" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

        <Button
            android:id="@+id/mirror_horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/mirror_horizontal"
            app:backgroundTint="#3F51B5" />

        <Button
            android:id="@+id/rotate_right"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/rotate_right"
            app:backgroundTint="#3F51B5" />

    </LinearLayout>

</LinearLayout>

阅读Layout Weight

Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0

要创建线性布局,其中每个 child 在屏幕上使用相同数量的 space,请设置 android:layout_height每个视图的 "0dp"(对于垂直布局)或每个视图的 android:layout_width"0dp"(对于水平布局)。

    <LinearLayout
    android:layout_width="400dp"
    android:layout_height="100dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toTopOf="@id/table_playfield"
    app:layout_constraintTop_toBottomOf="@id/table_tile2"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" 
    android:weightSum="6"
   
    >

    <com.example.se2_gruppenphase_ss21.game.TimerView
        android:id="@+id/timerView2"
        android:layout_width=“0dp”
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="2" >

        <Button
            android:id="@+id/remove"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/remove_tile"
            app:backgroundTint="#2196F3" />

        <Button
            android:id="@+id/ubongo_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:fontFamily="@font/architects_daughter"
            android:text="UBONGO !!"
            android:textSize="16sp"
            android:textStyle="bold"
            app:backgroundTint="#FF1010" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1" >

        <Button
            android:id="@+id/mirror_vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/mirror_vertical"
            app:backgroundTint="#3F51B5" />

        <Button
            android:id="@+id/rotate_left"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/rotate_left"
            app:backgroundTint="#3F51B5" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

        <Button
            android:id="@+id/mirror_horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/mirror_horizontal"
            app:backgroundTint="#3F51B5" />

        <Button
            android:id="@+id/rotate_right"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/rotate_right"
            app:backgroundTint="#3F51B5" />

    </LinearLayout>

</LinearLayout>
    

仅供参考

如果用android:weightSum就更好了。

Defines the maximum weight sum. 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.