【问题标题】:CardView custom style attributesCardView 自定义样式属性
【发布时间】:2018-04-20 02:54:07
【问题描述】:

我读过很多其他关于这些东西的问题,例如 thisthisthis 但没有人适合我。

我需要创建一个自定义样式表单 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>

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

【问题讨论】:

    标签: android xml android-cardview android-styles cardview


    【解决方案1】:
    <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放在scrollView里面
    【解决方案2】:

    不要在卡片视图中设置主题而不是样式

    改变这个

    android:theme="@style/CardViewStyle"
    

    style="@style/CardViewStyle"
    

    它的工作。

    【讨论】:

      猜你喜欢
      • 2011-01-11
      • 1970-01-01
      • 2011-12-12
      • 2015-01-23
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多