GridLayout仅显示第一行

时间:2017-11-01 00:17:34

标签: android android-layout

所以我试图在我的应用主屏幕中间为按钮获取GridLayout。我有3行2列,但它只显示第一行,其余图像按钮溢出屏幕边缘。

我想避免设置GridLayout的确切大小(我知道解决了这个问题),因为它可能会在很多屏幕上结束。

我有什么方法可以解决这个问题吗?

这是它正在做的事情:

incorrect layout

这应该是这样的:

correct layout

这是我的GridLayout代码:

<?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"
    android:background="@drawable/bg_gradient">

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:fontFamily="cursive"
    android:text="@string/appTitle"
    android:textAlignment="center"
    android:textSize="60sp"
    android:textStyle="bold"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/settingsButton"
    app:layout_constraintRight_toLeftOf="@+id/emergencyButton"/>

<GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_below="@+id/textView"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="32dp"
    android:layout_marginRight="32dp"
    android:layout_marginTop="16dp"
    android:columnCount="2"
    android:rowCount="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView">

    <ImageView
        android:id="@+id/notesImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/notepad" />

    <ImageView
        android:id="@+id/tasksImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/list" />

    <ImageView
        android:id="@+id/assignmentsImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/project" />

    <ImageView
        android:id="@+id/classesImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/blackboard" />

    <ImageView
        android:id="@+id/gradesImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/grades" />

    <ImageView
        android:id="@+id/achievementsImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/trophy" />
</GridLayout>

<ImageView
    android:id="@+id/settingsButton"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:clickable="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/settings"/>

<ImageView
    android:id="@+id/emergencyButton"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:clickable="true"
    app:srcCompat="@drawable/sos"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"/>


</android.support.constraint.ConstraintLayout>

我哪里错了?

3 个答案:

答案 0 :(得分:0)

更改

android:layout_width="0dp"

android:layout_width="match_parent" 

再次尝试与我合作

答案 1 :(得分:0)

您可以设置android:layout_height="0dp"

<GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    ...                       />

我添加了您未提供的TextView

在您的代码中尝试此操作。

<?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">

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="hello"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_below="@+id/textView"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="32dp"
    android:layout_marginLeft="32dp"
    android:layout_marginRight="32dp"
    android:layout_marginStart="32dp"
    android:layout_marginTop="16dp"
    android:columnCount="2"
    android:rowCount="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView">

    <ImageView
        android:id="@+id/notesImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/notepad"/>

    <ImageView
        android:id="@+id/tasksImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/list"/>

    <ImageView
        android:id="@+id/assignmentsImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/project"/>

    <ImageView
        android:id="@+id/classesImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/blackboard"/>

    <ImageView
        android:id="@+id/gradesImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/grades"/>

    <ImageView
        android:id="@+id/achievementsImageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/trophy"/>
</GridLayout>

</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

提供的代码在编辑器和设备上看起来完全正常。

检查您的应用编译依赖项:(EX)

compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:26.+'