首页 > 解决方案 > GridLayout 中的按钮对齐方式

问题描述

我的布局代码及其图形表示如下所示:

这是它的外观:

这是它的外观:

当我尝试在网格布局中放入按钮时,它不显示。我尝试将行数和列数设置为 2,但它似乎不起作用。

我想让布局看起来像这样:

我想让布局看起来像这样:

这是我的 xml 文件的样子:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/goButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="goClick"
        android:padding="50dp"
        android:text="GO!"
        android:textSize="40sp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:background="?android:attr/colorActivatedHighlight"
        android:padding="10dp"
        android:text="30s"
        android:textSize="30sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:background="?android:attr/textColorLinkInverse"
        android:padding="10dp"
        android:text="0/0"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:text="3 + 7 = ?"
        android:textSize="25sp"
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintHorizontal_bias="0.49"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.GridLayout
        android:layout_width="368dp"
        android:layout_height="474dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </android.support.v7.widget.GridLayout>

</android.support.constraint.ConstraintLayout>

我已经放了一个按钮,但它不可见:

我已经放了一个按钮,但它不可见:

谢谢你的回答

标签: javaandroidandroid-layoutandroid-gridlayout

解决方案


实际的 xml 文件是什么样的?这会更有帮助,但在看不到您的 xml 文件时,请确保所有按钮都在网格布局标签内。如果您想保留所有内容,甚至将列数和行数设置为 2,请将网格布局的宽度和高度设置为与父级匹配,然后在您的按钮上,您必须在每个按钮上将 layout_columnweight 和 layout_rowweight 设置为 1,以便它们只占用 1网格上的部分。尝试从那个开始,就像我说的那样,如果你发布你的 xml 文件,我就不能告诉你到底出了什么问题

它应该看起来像这样

<GridLayout xmlns:android="//schemas.android.com/apk/res/android"
    xmlns:app="//schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2">
    <Button
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="#color you want"
        android:textSize="size you want"
        android:text="text you want" />
    <Button
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="#color you want"
        android:textSize="size you want"
        android:text="text you want" />
    <Button
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="#color you want"
        android:textSize="size you want"
        android:text="text you want" />
    <Button
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="#color you want"
        android:textSize="size you want"
        android:text="text you want" />
</GridLayout>

推荐阅读