在 CardCornerRadius 和 CardElevation 上显示错误

Showing Errors on CardCornerRadius and CardElevation

我定义的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@drawable/background"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    tools:context=".MainActivity">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="32dp"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="16dp">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="16dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question1title"
                    style="@style/questionTitle"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question1"
                    style="@style/questionAndAnswers"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <CheckBox
                        android:id="@+id/checkboxQuest1AnswerMario"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/answerMario"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <CheckBox
                        android:id="@+id/checkboxQuest1AnswerDonkey"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/answerDonkeyKong"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <CheckBox
                        android:id="@+id/checkboxQuest1AnswerPortal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/answerPortal"/>
                </LinearLayout>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question2title"
                    style="@style/questionTitle"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question2"
                    style="@style/questionAndAnswers"/>

                <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <RadioButton android:id="@+id/radio_1979"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/answer1979"/>
                    <RadioButton android:id="@+id/radio_1989"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/answer1989"/>
                    <RadioButton android:id="@+id/radio_1999"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/answer1999"/>
                </RadioGroup>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question3title"
                    style="@style/questionTitle"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question3"
                    style="@style/questionAndAnswers"/>

                <EditText
                    android:id="@+id/answerInputUserLastName"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/LastNameLaraHint"
                    android:inputType="text"
                    android:maxLength="10"/>


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question4title"
                    style="@style/questionTitle"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question4"
                    style="@style/questionAndAnswers"/>

                <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <RadioButton android:id="@+id/radio_bioShock"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/answerBioShock"/>
                    <RadioButton android:id="@+id/radio_borderlands"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/answerBorderlands"/>
                    <RadioButton android:id="@+id/radio_portal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/answerPortal"/>
                </RadioGroup>


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question5title"
                    style="@style/questionTitle"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question5"
                    style="@style/questionAndAnswers"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <CheckBox
                        android:id="@+id/checkboxQuest5GameBoy"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/answerGameBoy"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <CheckBox
                        android:id="@+id/checkboxQuestPSP"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/answerPSP"/>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <CheckBox
                        android:id="@+id/checkboxQuest5Wii"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/answerWii"/>
                </LinearLayout>

                <Button
                    android:id="@+id/submitButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/submit"
                    android:layout_gravity="center_horizontal"/>
            </LinearLayout>

        </ScrollView>

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

</RelativeLayout>

我在 CardCornerRadiusCardElevation 中收到以下错误:

Android resource linking failed D:\AndroidStudioProjects\App\app\src\main\res\layout\activity_main.xml:12: AAPT: error: attribute cardCornerRadius (aka com.ksaitarun.app:cardCornerRadius) not found.

D:\AndroidStudioProjects\App\app\src\main\res\layout\activity_main.xml:12: AAPT: error: attribute cardElevation (aka com.ksaitarun.app:cardElevation) not found.

我不认为有什么问题,但可能有帮助的是为所有自定义属性使用一个名称空间。 在您的父容器中,您使用的是 app 命名空间

xmlns:app="http://schemas.android.com/apk/res-auto"

因此,您需要在 cardview 属性中使用该命名空间。只需将 card_view 替换为 app 并删除 CardView 中的命名空间。

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="32dp"
    app:cardCornerRadius="8dp"
    app:cardElevation="16dp">

确保您已将此库添加到 build.gradle 文件

implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'