【问题标题】:GridLayout is only showing the first rowGridLayout 只显示第一行
【发布时间】:2017-11-01 11:07:45
【问题描述】:

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

我想避免设置 GridLayout 的确切大小(我知道这可以解决问题),因为它可能会出现在许多屏幕上。

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

这是它的作用:

它应该是这样的:

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

我哪里出错了?

【问题讨论】:

  • 可能出现在许多屏幕上是什么意思?尝试将图像视图中的 rowWeights 更改为 0.33
  • 显示你的整个xml代码。
  • @Mars 它可能在旧版本的 Android 上,也可能在小屏幕、平板电脑上。我不想将应用程序限制在单一类型的设备上。
  • @Moira 您当前的目标和最低 SDK 是多少?
  • 使用您的代码,它看起来很好(至少在编辑器中)...这只是部署到设备时的问题吗?另外,您在布局编辑器中遇到了哪些错误/警告?

标签: android android-layout


【解决方案1】:

改变

android:layout_width="0dp"

android:layout_width="match_parent" 

再试一次,这对我有用

【讨论】:

    【解决方案2】:

    你可以设置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>
    

    【讨论】:

    • 抱歉,我在代码中输入了错误的高度并将其修复为无法正常工作的代码,这正是您在此处提到的。
    • 我已经用我的完整 XML 进行了更新,但它仍然被破坏了。
    • 你可以使用LinearLayout,效果更好。
    【解决方案3】:

    提供的代码在编辑器和设备上看起来都很好。

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

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

    【讨论】:

      猜你喜欢
      • 2017-01-15
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多