关于网格布局:android.support.v7.widget.GridLayout无法使其正常工作

android.support.v7.widget.GridLayout cannot get it to work

我有一个Android项目,该项目的大多数菜单和屏幕都使用GridLayout。但是,问题在于从API 14开始支持GridLayout。

由于我也想使该应用程序也可用于旧版本的android,因此我尝试使用Android自己的支持库GridLayout,该支持库增加了对API 7的支持。
这正是我一直在寻找的东西,但是我无法终生使它运转。我已经尝试了所有这些解释和想法:

  • Android的官方说明
  • 解决方案1
  • 解决方案2
  • 解决方案3
  • 解决方案4
  • 解决方案5
  • 还有更多...

    无论我做什么,怎么做或使用什么IDE(无论是Eclipse ADT还是Android Studio),它都会始终在Layout XML中给我一个错误,如下所示:

    1
    2
    3
    4
    5
    6
    The following classes could be instantiated:  - android.support.v7.widget.GridLayout

    With either one of these exceptions showing in the error log:

    1. android.content.res.Resources$NotFoundException: Could not resolve value 0x7F080000
    2. java.lang.classnotfoundexception: android.support.v7.gridlayout.R$dimen

    编辑:供参考,这是我用来创建支持gridlayout的内容(直接从android示例程序中获取):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <android.support.v7.widget.GridLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/blue"
        android:padding="10dip"
        app:columnCount="4"
        >
        <TextView
            android:text="@string/string_test"
        />
        <EditText
            app:layout_gravity="fill_horizontal"
            app:layout_column="0"
            app:layout_columnSpan="4"
        />
        <Button
            android:text="@string/button_test"
            app:layout_column="2"
        />
    </android.support.v7.widget.GridLayout>

    以上解决方案均无效,我可能会做错什么?我缺少什么吗,也许我的原始代码有问题?

    感谢您的帮助


    尝试使用Gradle,并在build.gradle文件的末尾添加以下部分:

    1
    2
    3
    4
    dependencies {
      implementation 'com.android.support:gridlayout-v7:28.0.0'
      implementation 'com.android.support:appcompat-v7:28.0.0'
    }

    然后执行assembleDebug gradle任务。


    使用Android Studio:

    • 进入build.gradle并添加:

      1
      compile 'com.android.support:appcompat-v7:18.0.+'

      在您的依赖项中。

    • 通过单击AVD管理器左侧的图标来同步项目。它将实现库

    • 然后重试

    使用Eclipse尝试以下操作:对较旧的api android.support.v7.widget.Gridlayout的Gridview v7支持无法实例化


    从http://developer.android.com/tools/support-library/features.html#v7-gridlayout引用,您应该将gridlayout作为eclipse项目导入,然后将其作为库项目添加到您的项目中,确保构建路径包含jar。


    对于当前稳定的1.0.0版:
    要添加对GridLayout的依赖关系,必须将Google Maven存储库添加到项目中。
    在您的应用程序或模块的build.gradle文件中添加所需工件的依赖项:

    1
    2
    3
    dependencies {
        implementation"androidx.gridlayout:gridlayout:1.0.0"
    }


    在我的情况下,我修复了该设置:

    1
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" />

    在gridlayout_v7项目清单文件中。