使GridView适合其容器/约束

时间:2018-02-17 02:33:37

标签: android gridview android-constraintlayout

我正在制作小型的内存游戏。 关于GridView内部约束布局,我一直在讨论这个问题几个小时。我希望它适合它的约束,而不需要滚动。

有没有办法让GridView完全适合容器内部,所以水平轴是否准确?底边应与@+id/text_time上边缘匹配。 Internet和Stackoverflow充满了这些示例,但对于ImageView.ScaleType。 下面是一个简短的布局代码:

<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="@color/colorPrimary"
tools:context="com.example.alen.matchinggameaddiko.activities.GameActivity">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="@dimen/gameLogoSize"
    android:layout_height="0px"
    android:layout_marginLeft="@dimen/gameLogoMarginLeft"
    android:layout_marginTop="@dimen/gameLogoMarginTop"
    android:src="@drawable/addiko_logo_white"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<GridView
    android:id="@+id/grid_game"
    android:layout_width="0px"
    android:layout_height="0px"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="@dimen/gameGridMarginTop"
    android:gravity="center"
    android:horizontalSpacing="@dimen/gameGridHorizontalSpacing"
    android:numColumns="4"
    android:verticalSpacing="@dimen/gameGridVerticalSpacing"
    app:layout_constraintBottom_toTopOf="@id/text_time"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView" />

<TextView
    android:id="@+id/text_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/timerMarginBottom"
    android:layout_marginRight="@dimen/timerMarginRight"
    android:textColor="@color/colorText"
    android:textSize="@dimen/timerTextSize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

Here is a photo。我绘制一条红线,因此GridView应该结束的位置更加清晰。 非常感谢你们。

1 个答案:

答案 0 :(得分:0)

我已经看过你的Layout.xml尝试在宽度和高度都设置你的GridView,Match_Parent。在constraintEnd_toEndOf上设置toStartoftoTopof以及"parent"等等。对于GridView。

然后将那些Textview和ImageView设置为与GridView对齐。

您所做的是首先设置ImageView和TextView然后使用它们对齐GridView,我建议将GridView设置为与Parent匹配,然后将其他任何内容与GridView对齐。

此外,您必须检查XML是否没有为GridView的高度提供任何警告。您可能必须将GridView高度设置为WRAP_PARENT。只需检查