我在@drawable \ button.xml
中有以下代码 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@drawable/button_pressed"
/>
</selector>
以及@ drawable / button_pressed.xml
中的代码 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:foregroundTint="@color/cardview_shadow_start_color"
/>
</shape>
@ layouts / cardview.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:orientation="horizontal"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
android:layout_margin="5dp"
android:layout_gravity="center_horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:focusable="false"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fffffff0"
>
<ImageView
android:id="@+id/movieIconCard"
android:layout_width="80dp"
android:layout_height="100dp"
android:scaleType="fitXY"/>
<ImageView
android:id="@+id/selectionCard"
android:src="@drawable/optionsmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
<TextView
android:id="@+id/movieTitleCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_toEndOf="@id/movieIconCard"
android:layout_toStartOf="@id/selectionCard"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="@+id/descriptionCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:lines="3"
android:ellipsize="end"
android:layout_below="@id/movieTitleCard"
android:layout_toEndOf="@id/movieIconCard"
android:layout_toStartOf="@id/selectionCard"
android:layout_marginLeft="10dp"/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingCard"
android:stepSize="0.1"
style="?android:attr/ratingBarStyleSmall"
android:layout_below="@id/descriptionCard"
android:layout_toEndOf="@id/movieIconCard"
android:isIndicator="true"
android:rating="1"
android:visibility="visible"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:id="@+id/ratingTextCard"
android:layout_toRightOf="@id/ratingCard"
android:layout_alignBottom="@id/ratingCard"
android:layout_alignTop="@+id/ratingCard"
android:textAlignment="gravity"
android:gravity="center"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
无论是否选择了state_pressed / focused / hovered,都无法改变cardview的外观
答案 0 :(得分:0)
CardView不允许自定义背景绘制。它只接受背景颜色,我建议您使用静态方法来改变cardView的状态,例如:
public static void setCardBackground(CardView cardView, int color){
if(color == 0) return;
cardView.setCardBackgroundColor(color);
}