【问题标题】:Fill whole screen with four buttons by two rows in Android在Android中用两行四个按钮填充整个屏幕
【发布时间】:2022-03-16 00:06:38
【问题描述】:

我想用四个具有相同宽度和高度的按钮来填充整个屏幕,所以我认为网格布局是个好主意:

<GridLayout
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_columnWeight="1"
        android:layout_rowWeight="0"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_columnWeight="1"
        android:layout_rowWeight="0"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        />

</GridLayout>

不幸的是,我的结果如下所示:

不明白为什么第二排的高度这么大?
我认为网格布局中的每个单元格都有相同的宽度和高度!

【问题讨论】:

  • 更新您的图片链接。打不开
  • 显然是因为rowWeight不同
  • 尝试将所有 rowWeights 设置为 1
  • 是的,你说得对,但我真的不明白为什么它们现在不应该排成一行?我以为我需要两行...
  • 事实上,正如您所说,现在它可以工作了。这意味着您最终得到了一个完美分割的 2x2 网格。这就是权重的工作原理:如果您希望它们均匀分布,它们必须彼此均匀。就这么容易。不是吗?

标签: android android-layout height width android-gridlayout


【解决方案1】:

这也可以使用 ConstraintLayout 来完成,这将为您提供扁平的层次结构和更大的灵活性以应对未来的变化。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintBottom_toTopOf="@id/button3"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button4" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintTop_toBottomOf="@id/button2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button3"/>
</android.support.constraint.ConstraintLayout>

【讨论】:

    【解决方案2】:

    如果你想用 Gridlayout 做到这一点:

        <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="2"
        android:orientation="vertical"
        android:rowCount="2">
    
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1" />
    
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1" />
    </GridLayout>
    

    你可以用这样的线性布局来做到这一点:

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

    【讨论】:

    • 首选第一个解决方案,因为它的平坦度。第二个是。因为嵌套布局会导致性能不佳。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    相关资源
    最近更新 更多