CardView 自定义样式属性

CardView custom style attributes

我读了很多关于这个东西的其他问题,比如 this or or 但没有一个适合我。

我需要创建自定义样式表单 CardView。这是我的代码:

style.xml

<resources >    
 ...
    <style name="CardViewStyle" parent="CardView">
        <item name="app:cardBackgroundColor">#d9edf7</item>
    </style>    
</resources>

my_card.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/CardViewStyle">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txvId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/txvDatetime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

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

在我的应用程序中 gradle 文件

compileSdkVersion 26
minSdkVersion 19
targetSdkVersion 26
implementation 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'

这不太可能对我不起作用。没有任何反应,但我没有发现问题所在。 如果我不用自定义样式直接做:

my_card.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardBackgroundColor="#d9edf7" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txvId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/txvDatetime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

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

它工作正常。但是我需要使用自定义样式。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="6dp"
    tools:context="in.a.a.a.a">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView_sylb"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:lineSpacingExtra="8sp"
            android:padding="5dp"
            android:text="Coming soon"
            android:textAlignment="textStart"
            android:textIsSelectable="false"
            android:textSize="14sp"
            android:typeface="serif" />


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


</ScrollView> 

不要在 cardview 中设置主题而不是样式

改变这个

android:theme="@style/CardViewStyle"

style="@style/CardViewStyle"

成功了。