Android LinearLayout layout_weight behavior



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

现在我需要使最后两列的大小是前两列的一半。因此,我使用layout_weight,并将其设置为1表示最后两个,将其设置成2表示前两个。然而,这会导致最后两列占据整个空间。

我想这一定与其他布局设置有关,但由于我缺乏经验,我无法完全理解。希望你们中的一个人能帮我。

这是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

指示LinearLayout中分配了多少额外空间到与这些LayoutParams关联的视图。如果视图不应拉伸。否则,额外像素将按比例计算在权重大于0 的所有视图中

要创建一个线性布局,其中每个子级在屏幕上使用相同的空间,请将每个视图的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>

FYI

如果使用android:weightSum会更好。

定义最大权重总和。如果未指定,则通过添加所有子项的layout_weight。这可以用于实例,通过将layout_weight设置为0.5,并将weightSum设置为1.0。

最新更新