【问题标题】:imageView does not display on the preview scence but still displays on the emulatorimageView 不显示在预览场景中,但仍显示在模拟器上
【发布时间】:2018-06-29 04:30:23
【问题描述】:

我正在尝试使用 GridLayout 创建像井字游戏这样的游戏。预览屏幕可以显示 GridLayout(板)的背景图片,但无法显示我在 GridLayout(红色 O)中插入的 imageView。消息框里还有一个错误说:

java.lang.ClassNotFoundException: android.view.View$OnUnhandledKeyEventListener

我已尝试刷新布局并重建项目,但仍然无法修复错误。

预览屏幕与显示两个图像的模拟器屏幕比较

activity_main.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">

    <android.support.v7.widget.GridLayout
        android:layout_width="368dp"
        android:layout_height="368dp"
        android:background="@drawable/board"
        app:columnCount="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rowCount="3">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            app:srcCompat="@drawable/red" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            app:srcCompat="@drawable/red" />
    </android.support.v7.widget.GridLayout>
</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 您使用的是哪个版本的 com.android.support?

标签: java android android-studio android-studio-3.1.3


【解决方案1】:

尝试此代码并仅更改 GridLayout 采用.. 您可以更改根布局而不是 constraintLayout 采用任何布局。

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

<GridLayout
    android:layout_width="368dp"
    android:layout_height="368dp"
    android:columnCount="3"
    android:rowCount="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/user" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/user" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/user" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@drawable/user" />

</GridLayout>

【讨论】:

    【解决方案2】:

    为网格布局设置方向

    <GridLayout
        android:columnCount="3"
        android:rowCount="3"
        android:orientation="horizontal"
         />
    

    【讨论】:

      【解决方案3】:

      只需更改您的&lt;android.support.v7.widget.GridLayout

      <GridLayout
              android:layout_width="368dp"
              android:layout_height="368dp"
              android:background="@drawable/board"
              android:columnCount="3"
              android:rowCount="3"
              android:orientation="horizontal"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent">
      
              <ImageView
                  android:id="@+id/imageView"
                  android:layout_width="100dp"
                  android:layout_height="100dp"
                  android:layout_margin="10dp"
                  app:srcCompat="@drawable/red" />
      
              <ImageView
                  android:id="@+id/imageView2"
                  android:layout_width="100dp"
                  android:layout_height="100dp"
                  android:layout_margin="10dp"
                  app:srcCompat="@drawable/red" />
          </GridLayout>
      

      这是给我的预览。试试这个。 还要检查依赖库版本
      implementation 'com.android.support.constraint:constraint-layout:1.1.0'

      【讨论】:

        【解决方案4】:

        在我的 Android Studio 中,您的 xml 既显示在预览版中,也显示在模拟器中。尝试执行以下操作。

        1) 再次添加 build.gradle(Module.app) 这些依赖项

        implementation 'com.android.support:gridlayout-v7:27.1.1'
        implementation 'com.android.support:appcompat-v7:27.1.1'
        

        2)成功导入依赖后,刷新Android Studio

        希望它有效!

        【讨论】:

          【解决方案5】:

          对于那些偶然发现此错误的人。仅在 XML 中将 android.support.v7.widget.GridLayout 更改为 GridLayout。我有同样的问题,这就是解决方案。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-30
            • 2018-08-17
            • 1970-01-01
            • 1970-01-01
            • 2013-05-13
            • 1970-01-01
            相关资源
            最近更新 更多