CardView

时间:2015-06-26 19:00:51

标签: android xml

对于我设置为可点击的CardView,我遇到了一些奇怪的行为。点击它会略微改变卡内部的所有视图,这使得当您停止按卡时看起来很奇怪。

Here is what it looks like when the card is clicked。理想情况下,当手指放在上面时,我希望整张卡片变成粉红色。当你松开时,卡片会均匀地消失为白色。

感谢任何建议!

card_class.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cv"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_margin="5dp"
    android:clickable="true"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:background="@drawable/button_rect_list_normal">
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/RelativeLayout01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:clickable="false"
                android:background="@drawable/button_rect_list_normal">

                <TextView
                    android:id="@+id/class_name"
                    android:gravity="center_vertical"
                    android:paddingStart="16dp"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp"
                    android:paddingTop="18dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="24sp"
                    android:textColor="@color/text"
                    android:clickable="false"
                    android:text="@string/classname_placeholder"
                    android:background="@drawable/button_rect_list_normal"
                    android:paddingBottom="18dp"
                    android:autoText="true" />

                <TextView
                    android:id="@+id/class_teacher"
                    android:gravity="center_vertical"
                    android:paddingStart="16dp"
                    android:paddingLeft="16dp"
                    android:paddingRight="16dp"
                    android:paddingBottom="24dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="16sp"
                    android:textColor="@color/secondary2"
                    android:clickable="false"
                    android:text="@string/teacher_placeholder"
                    android:background="@drawable/button_rect_list_normal" />
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="@string/period_placeholder"
                android:id="@+id/class_period"
                android:elegantTextHeight="false"
                android:singleLine="true"
                android:paddingRight="16dp"
                android:textSize="57sp"
                android:gravity="center_vertical" />
        </RelativeLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

button_rect_list_normal.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <item android:drawable="@drawable/selected" android:state_selected="true"/>
    <item android:drawable="@drawable/pressed" android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

pressed.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#F44336"/>
</shape>

selected.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFCDD2"/>
</shape>

1 个答案:

答案 0 :(得分:2)

如果您希望整张卡片的背景有所改变,您应该将您的drawable应用于根CardView - 我不确定为什么您有多个ViewGroup的背景绘制副本应用于

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cv"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_margin="5dp"
    android:clickable="true"
    android:foreground="@drawable/button_rect_list_normal"
    android:layout_height="wrap_content">

编辑:android:background更改为android:foreground