Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GridLayout中的按钮对齐_Java_Android_Android Layout_Android Gridlayout - Fatal编程技术网

Java GridLayout中的按钮对齐

Java GridLayout中的按钮对齐,java,android,android-layout,android-gridlayout,Java,Android,Android Layout,Android Gridlayout,我的布局代码及其图形表示如下所示: 这就是它的样子: 当我尝试在网格布局中插入按钮时,它不会显示。我尝试将行计数和列计数设置为2,但似乎不起作用 我想让布局看起来像这样: 这就是我的xml文件的外观: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/andr

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

这就是它的样子:

当我尝试在网格布局中插入按钮时,它不会显示。我尝试将行计数和列计数设置为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>

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


谢谢你的回答

你可以用android:gravity=“属性来做,请看下面的代码,它认为这是你想要的

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2">

<Button
    android:layout_width="0dp"
    android:layout_height="100dp"
    android:layout_columnWeight="1"
    android:gravity="center"
    android:layout_gravity="fill_horizontal"
    android:background="@color/colorAccent"
    android:text="Title1" />

    <Button
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:layout_gravity="fill_horizontal"
        android:background="@color/colorPrimaryDark"
        android:text="Title2" />

        <Button
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:gravity="center"
            android:layout_gravity="fill_horizontal"
            android:background="@color/colorPrimary"
            android:text="Title3" />

            <Button
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_columnWeight="1"
                android:gravity="center"
                android:layout_gravity="fill_horizontal"
                android:background="@color/colorAccent"
                android:text="Title4" />

 </GridLayout>

实际的xml文件是什么样子的?这会更有帮助,但在看不到xml文件时,请确保所有按钮都位于网格布局标记内。将网格布局的宽度和高度设置为与父级匹配如果要使所有内容保持均匀,请将列数和行数设置为2,然后在按钮上,必须将每个按钮上的layout_columnweight和layout_rowweight设置为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>

请发布布局文件。