60

簡単に言えば:

CardView の cardBackgroundColor プロパティの色の状態を定義するにはどうすればよいでしょうか (この場合は ListView レイアウトで)。

(4.4 がインストールされた電話で Android L 開発者プレビューの RC1 を使用し、build.gradle で「com.android.support:cardview-v7:21.0.0-rc1」を使用しています)

より長いです:

CardView レイアウトでは、cardCornerRadius と cardBackgroundColor を介して CardView の角の半径と背景色を設定します。

ただし、背景色は選択状態を反映しません。たとえば、リスト項目が押された場合などです。

ただし、CardView の内側のビューで、背景色と関連する状態を設定すると、CardView で定義した角の上に表示されます。

では、CardView の cardBackgroundColor の状態が尊重されるようにするにはどうすればよいでしょうか?

cardBackgroundColor、color_with_states.xml に使用される色は次のとおりです。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:color="@android:color/holo_green_dark" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:color="@android:color/holo_green_dark" />
    <item android:state_focused="true"                                android:state_pressed="true" android:color="@android:color/holo_green_dark" />
    <item android:state_focused="false"                               android:state_pressed="true" android:color="@android:color/holo_green_dark" />
    <item android:state_focused="true"                                                             android:color="@android:color/holo_green_dark" />
    <!-- Only this below is seen in the cardview dispaly -->
    <item android:color="@android:color/holo_blue_bright" />
</selector>

そして、CardView を使用するレイアウト:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    cardview:cardCornerRadius="10dp"
    cardview:cardBackgroundColor="@color/colour_with_states"
    >

<!-- If we set a background color below, it will overwrite our radius defined above -->
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:text="Lorem ipsum"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:background="@null"
    android:gravity="center_vertical"
    android:paddingTop="8dip"
    android:paddingBottom="8dip"
    android:paddingStart="8dip"
    android:paddingEnd="8dip"
    />

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

9 に答える 9

12

CardView視覚的なタッチ フィードバックが必要な場合があります。ソリューションはこれandroid:foreground="?android:attr/selectableItemBackground"に最適です。

ただし、drawSelectorOnTop(true)ListView での使用を検討することもできます。これは、まったく変更する必要はありませんCardView

さらに説明が必要な場合はお知らせください。

于 2015-03-27T22:40:57.407 に答える
6

これがあなたの問題を解決する私の方法です。

まず、CustomCardViewextendsという名前のカスタム クラスを作成します。CardView

次に、メソッドをオーバーライドし、カードのプレス状態が変化したときにメソッドdrawableStateChanged()を呼び出してカードの背景色を変更します。setCardBackgroundColor()

最後に、レイアウト ファイルで CardView をこの CustomCardView に置き換えます。

このソリューションの唯一の欠点は、cardview が Android 5.0 以降でリップル プレス効果を表示できないことです。

ここに私のコードがあります:

public class CustomCardView extends CardView {

public CustomCardView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public CustomCardView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public CustomCardView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // TODO Auto-generated constructor stub
}

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (isPressed()) {
        this.setCardBackgroundColor(getContext().getResources().getColor(R.color.card_view_pressed));
    } else {
        this.setCardBackgroundColor(getContext().getResources().getColor(R.color.card_view_normal));
    }
}

}

于 2015-06-10T12:59:11.977 に答える
3

**カードビュー内に行を追加するだけ**

    android:clickable="true"
    android:focusableInTouchMode="true"
    android:foreground="?android:attr/selectableItemBackground"
于 2016-03-02T09:49:06.963 に答える
0

カードビューと同じ角の半径を持つ長方形を使用しました。そして、カードビューの内部ビューの背景として xml ドローアブルを使用しました。背景はカードビューのコーナーには表示されませんが、カードとその内側のビューの間に小さなパディングがまだあります。

ここに画像の説明を入力

于 2015-11-18T16:43:19.763 に答える