【问题标题】:Positioned Grid View pictures dont appear in cell定位网格视图图片不出现在单元格中
【发布时间】:2018-03-21 20:11:53
【问题描述】:

我用图像制作了一个迷你游戏。我想使用网格布局边距作为板来保持动态大小。现在,当我出现图片时,它们出现了问题,它们以全尺寸出现,而不是在网格单元格中。

这是我的活动主要 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="com.example.codes.logicgarden.MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="false"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="0dp">



                <ImageView
                    android:id="@+id/im00"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_column="0"
                    android:layout_columnWeight="1"
                    android:layout_row="0"
                    android:layout_rowWeight="1"
                    android:adjustViewBounds="true"
                    android:background="@android:color/background_light"
                    android:onClick="dropIn"
                    android:tag="0" />


            <ImageView
                android:id="@+id/im01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_row="0"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:adjustViewBounds="true"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="1" />

            <ImageView
                android:id="@+id/im02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_row="0"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:adjustViewBounds="true"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="2" />

            <ImageView
                android:id="@+id/im03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:layout_row="0"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="3" />

            <ImageView
                android:id="@+id/im04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="4"
                android:layout_row="0"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="4" />

            <ImageView
                android:id="@+id/im10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="5" />

            <ImageView
                android:id="@+id/im11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="6" />

            <ImageView
                android:id="@+id/im12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="7" />

            <ImageView
                android:id="@+id/im13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="8" />

            <ImageView
                android:id="@+id/im14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="4"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="9" />

            <ImageView
                android:id="@+id/im20"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="10" />

            <ImageView
                android:id="@+id/im21"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_row="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="11" />

            <ImageView
                android:id="@+id/im22"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_row="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="12" />

            <ImageView
                android:id="@+id/im23"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:layout_row="2"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="13" />

            <ImageView
                android:id="@+id/im24"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="4"
                android:layout_columnWeight="1"
                android:layout_row="2"
                android:layout_rowWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="14" />

            <ImageView
                android:id="@+id/im30"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="3"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="15" />

            <ImageView
                android:id="@+id/im31"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_row="3"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="16" />

            <ImageView
                android:id="@+id/im32"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_row="3"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="17" />

            <ImageView
                android:id="@+id/im33"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:layout_row="3"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="18" />

            <ImageView
                android:id="@+id/im34"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="4"
                android:layout_row="3"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="19" />

            <ImageView
                android:id="@+id/im40"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="4"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="20" />

            <ImageView
                android:id="@+id/im41"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_row="4"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="21" />

            <ImageView
                android:id="@+id/im42"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_row="4"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="22" />

            <ImageView
                android:id="@+id/im43"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:layout_row="4"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="23" />

            <ImageView
                android:id="@+id/im44"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="4"
                android:layout_row="4"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="24" />

            <ImageView
                android:id="@+id/im50"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="5"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="25" />

            <ImageView
                android:id="@+id/im51"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_row="5"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="26" />

            <ImageView
                android:id="@+id/im52"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_row="5"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="27" />

            <ImageView
                android:id="@+id/im53"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:layout_row="5"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="28" />

            <ImageView
                android:id="@+id/im54"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="4"
                android:layout_row="5"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="29" />

            <ImageView
                android:id="@+id/im60"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_row="6"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="30" />

            <ImageView
                android:id="@+id/im61"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_row="6"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="31" />

            <ImageView
                android:id="@+id/im62"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:layout_row="6"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="32" />

            <ImageView
                android:id="@+id/im63"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="3"
                android:layout_row="6"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="33" />

            <ImageView
                android:id="@+id/im64"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="4"
                android:layout_row="6"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:background="@android:color/background_light"
                android:onClick="dropIn"
                android:tag="34" />

        </GridLayout>

        <RelativeLayout
            android:id="@+id/playAgainLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/gridLayout"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginBottom="100dp"
            android:background="@android:color/holo_green_dark"
            android:visibility="invisible">

            <TextView
                android:id="@+id/winnerMessage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="204dp"
                android:onClick="playAgain"
                android:text="Play Again" />

        </RelativeLayout>

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

当我点击它们将出现在网格视图单元格中的图像时,我该怎么做?

程序在图像具有固定大小但我想让它在所有平台上动态时工作。

How should look

How appear in emulator

【问题讨论】:

    标签: android gridview imageview


    【解决方案1】:

    您应该真正使用带有自定义适配器的 gridView。比你用点击监听器设置适配器,就是这样。

    因为如果您想更改图像中的某些内容,您只需执行一次而不是 32 次。

    这里有一个非常好的教程,可以让你开始使用https://www.raywenderlich.com/127544/android-gridview-getting-started

    【讨论】:

    • 我对问题进行了排序 android:layout_width="wrap_content" android:layout_height="wrap_content" 我将 wrap_content 更改为 1dp 现在可以工作了。谢谢你的链接,我期待着做这个教程!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多