gridlayout不会在api 22上显示,并且不能使用android.support.v7.widget.gridlayout

mm9b1k5b  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(352)

我在和api 21 emulator上进行测试,我意识到我的2个gridlayouts没有出现,但在api 30上运行良好。
我试着从 GridLayoutandroid.support.v7.widget.GridLayout ,但我得到一个 Cannot resolve class android.support.v7.widget.GridLayout 错误。
这是我的 activity.xml .

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:screenOrientation="portrait"

    tools:context=".GameActivity">

    <android.support.v7.widget.GridLayout
        android:id="@+id/play_board"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:columnCount="10"
        android:rowCount="10"
        app:layout_columnWeight="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toStartOf="parent">

    </android.support.v7.widget.GridLayout>

    <GridLayout
        android:id="@+id/my_board"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="64dp"
        android:layout_marginTop="64dp"
        android:layout_marginEnd="64dp"
        android:layout_marginBottom="64dp"
        android:columnCount="10"
        android:rowCount="10"
        app:layout_constraintBottom_toTopOf="@+id/play_board"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent">

    </GridLayout>

    <TextView
        android:id="@+id/my_board_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_board"
        app:layout_constraintBottom_toTopOf="@+id/my_board"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/attack_board_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/attack_board"
        app:layout_constraintBottom_toTopOf="@+id/play_board"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/swap_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/swap_message_text"
        android:textSize="40sp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的build.gradle(模块:app)

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "guilherme.battlleship"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.0'

    implementation 'com.android.support:gridlayout-v7:27.1.1'
    implementation 'com.android.support:support-v4:13.0.0'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

如您所见,我尝试导入gridlayout-v7,但没有成功。它的构建没有任何问题,但是当我启动包含gridlayout视图的活动时,我得到以下错误: Error inflating class android.support.v7.widget.GridLayout 提前谢谢。
编辑:
忘了提一下,但我在每个gridlayout中添加了多个TextView,使用以下循环:

for (int i = 0; i < gameBoard.size(); i++) {
            for (int j = 0; j < gameBoard.get(i).size(); j++) {

                GridLayout.LayoutParams param = new GridLayout.LayoutParams(
                        GridLayout.spec(GridLayout.UNDEFINED, 1f),
                        GridLayout.spec(GridLayout.UNDEFINED, 1f)
                );

                param.width = 0;
                param.height = 0;

                Spot boardSpot = gameBoard.get(i).get(j);

                TextView spot = new TextView(this);

                spot.setTag(i + "," + j);
                spot.setBackground(boardSpot.getShape());
    .
    .
    .
    .
                board.addView(spot, param);
            }
        }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题