添加selectableItemBackground并着色背景

时间:2015-11-19 15:37:18

标签: android material-design android-recyclerview android-cardview

我已在cardView上添加了selectableItemBackground,它显示在recyclerview上。 每个CardView显示文本和图像。 可以选择每张卡,并且为了在点击时添加涟漪效果,我添加了:

android:background="?attr/selectableItemBackground"

问题是现在我无法设置背景颜色。这两行一起显示错误:

android:background="?attr/selectableItemBackground"
android:background="#FFFFFF"

如何为布局背景着色,并使用selectableItemBackground添加涟漪效果。

完成card_view.xml代码:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="2dp"
    card_view:cardCornerRadius="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/selectableItemBackground"
        android:orientation="vertical">
        <!--CANT ADD THIS: android:background="#FFFFFF"-->

        <TextView
            android:id="@+id/singleTextLine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="3dp"
            android:text="Test"
            android:textSize="30dp" />

        <ImageView
            android:id="@+id/singleImage"
            android:layout_width="fill_parent"
            android:layout_height="125dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="3dp" />

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

问题是我在将attr / selectableItemBackground组合起来用于涟漪效果时无法为背景着色,然后它看起来像这样: enter image description here

3 个答案:

答案 0 :(得分:11)

您有两个选择:

1:添加父线性布局:

父布局有backgroundcolor和 子布局具有涟漪效应

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:orientation="vertical">

       <TextView
            android:id="@+id/singleTextLine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="3dp"
            android:text="Test"
            android:textSize="30dp" />

        <ImageView
            android:id="@+id/singleImage"
            android:layout_width="fill_parent"
            android:layout_height="125dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="3dp" />


    </LinearLayout>
</LinearLayout>

2:Opton: 创建可绘制资源:bg_ripple.xml

文件夹 drawable

中的一个
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/gray"/>
    <item android:drawable="@color/white"/>
</selector>

另一个文件夹 drawable-v21

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/gray">
    <item android:drawable="@color/white" />
</ripple>

然后将此资源设置为布局的背景:

android:background="@drawable/bg_ripple"

答案 1 :(得分:1)

另一种简单方法是再添加一个FrameLayout(设置背景颜色)并包含原始TextView

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/category_numbers">
<TextView
    android:id="@+id/numbers"
    style="@style/CategoryStyle"
    android:background="?android:selectableItemBackground"
    android:text="@string/category_numbers" />
</FrameLayout>

答案 2 :(得分:0)

现在我们可以以更好的方式实现这一目标。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" //ACTUAL BACKGROUND
    android:foreground="?selectableItemBackground" //RIPPLE EFFECT
    android:orientation="vertical">