不能在Android Studio 3上使用cardview进行编译

时间:2017-12-02 01:02:01

标签: android xml android-cardview

我正在使用Android Studio 3和AppCompat 它没有用这3个标签编译:

card_view:cardCornerRadius="6dp"
card_view:cardElevation="10dp"
card_view:cardUseCompatPadding="true"

这是我的XML代码我不知道我的依赖项中有什么错误我有AppCompat和CardView:

<?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.valenzuela.salvador.apuebacalculo.MainActivity">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        card_view:cardCornerRadius="6dp"
        card_view:cardElevation="10dp"
        card_view:cardUseCompatPadding="true" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/diferencial"
            android:scaleType="centerCrop"/>

        <TextView
            android:id="@+id/txt2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Calculo Diferencial"
            android:layout_gravity="bottom"
            android:background="#8c000000"
            android:textColor="#ffe3e3e3"
            android:textSize="30sp"
            android:textStyle="bold"/>

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

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:1)

您正在宣布&#34; auto&#34;此行上的命名空间:

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

但你正试图像这样使用它:

card_view:cardCornerRadius="6dp"
card_view:cardElevation="10dp"
card_view:cardUseCompatPadding="true"

改变这两者中的哪一个并不重要,但它们必须匹配。因此,您需要xmlns:card_viewapp:cardCornerRadius等。我建议使用app名称;这是标准做法。

答案 1 :(得分:0)

  

尝试添加此

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

代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto" <--add this line -->
    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.valenzuela.salvador.apuebacalculo.MainActivity">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        card_view:cardCornerRadius="6dp"
        card_view:cardElevation="10dp"
        card_view:cardUseCompatPadding="true" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/diferencial"
            android:scaleType="centerCrop"/>

        <TextView
            android:id="@+id/txt2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Calculo Diferencial"
            android:layout_gravity="bottom"
            android:background="#8c000000"
            android:textColor="#ffe3e3e3"
            android:textSize="30sp"
            android:textStyle="bold"/>

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

</android.support.constraint.ConstraintLayout>