我是新的材料卡片视图,我想要改变卡的前景颜色编程时,它是长按。但是setCardForegroundColor()方法的参数需要传递一个ColorStateList实例,而我不知道如何根据需要创建ColorStateList实例。
那么谁能告诉我,当cardview被长按时,如何使用ColorStateList来突出显示它?
发布于 2021-06-05 04:27:40
我没有使用ColorStateList,而是在列表项的background中使用drawable项来更改按下的RecyclerView项的颜色。下面是我如何实现它的:
列表项示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_selector"
android:padding="8dp">
<!-- Above: android:background defines the list item Touch Selector to highlight row -->
<TextView
android:id="@+id/rating_name_textview"
style="@style/EditorFieldStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textColor="@color/colorStatListItemText"
android:textSize="18sp" />
<TextView
android:id="@+id/rating_score_textview"
style="@style/EditorFieldStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textColor="@color/colorStatListItemText"
android:textSize="18sp"
tools:text="100" />
</LinearLayout>列表项选择器示例:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Adding Touch Selectors for when items in the RecyclerView are touched -->
<!-- Three states: pressed, activated, and selected -->
<item android:drawable="@color/colorPrimaryLight" android:state_pressed="true" />
<item android:drawable="@color/colorPrimaryLight" android:state_activated="true" />
<item android:drawable="@color/colorPrimaryLight" android:state_selected="true" />
<!-- Define background color for Touch Selectors when item is not selected (light gray) -->
<item android:drawable="@color/colorBackgroundLight" />
</selector>列表项引用列表项选择器,并根据我在颜色值文件中定义的颜色适当地突出显示RecyclerView项。
https://stackoverflow.com/questions/67837772
复制相似问题