12

在最新的 Android SDK 中,我们现在有了新的 CardView,我已经用新版本替换了旧的 CustomCardView,但是在旧版本的 Android 上运行时,我看到state_pressed&state_focused是出现在 CardView 上方的丑陋方块...

有谁知道我如何在新的 CardView 中模拟以下内容,但仅在使用旧版本的 Android 时?

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:enterFadeDuration="150"
    android:exitFadeDuration="150" >

    <item android:state_pressed="true"
          android:drawable="@drawable/card_on_press"/>
    <item android:state_focused="true" android:state_enabled="true"
          android:drawable="@drawable/card_on_focus"/>
    <item android:state_enabled="true"
          android:drawable="@drawable/card_default"/>
    <item android:state_enabled="false"
          android:drawable="@drawable/card_on_press"/>

</selector>

对于那些对这里感兴趣的人,我现在正在使用 CardView:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/CardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:onClick="RunSomeMethod"
    card_view:cardCornerRadius="4dp"
    android:focusable="true"
    android:elevation="2dp">

    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="false"
        android:visibility="visible">

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

3 回答 3

14

使用以下代码修复:

值-v21\styles.xml:

<style name="CardViewStyle" parent="CardView.Light">
    <item name="android:foreground">?android:attr/selectableItemBackground</item>
</style>

值\styles.xml:

<style name="CardViewStyle" parent="android:Widget.TextView">
    <item name="android:foreground">@drawable/card</item>
</style>

来自 layout\main_layout.xml 的卡片:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/Card_View"
    style="@style/CardViewStyle"
    android:layout_width="480dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    card_view:cardCornerRadius="4dp"
    android:elevation="2dp">

这将允许您在 v21+ 中提供动画,但也可以在 v21 之前提供替代动画,而不是大的蓝色/灰色方块。

这是我用作绘制对象的 card.xml,供您感兴趣的人使用:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:enterFadeDuration="150"
    android:exitFadeDuration="150" >

    <item android:state_pressed="true"
          android:drawable="@drawable/card_on_press"/>
    <item android:state_focused="true" android:state_enabled="true"
          android:drawable="@drawable/card_on_focus"/>
    <item android:state_enabled="true"
          android:drawable="@drawable/card_default"/>
    <item android:state_enabled="false"
          android:drawable="@drawable/card_on_press"/>
</selector>

注意:Drawable 默认可以作为透明项目,因为 CardView 为所有 android 版本提供了默认背景。

于 2014-07-06T06:07:51.030 回答
4

也许该库已更新,但是当我使用以下内容时,它在旧版本的 Android 上看起来不错。

您可以使用“CardView.Dark”和“CardView.Light” (将使用卡片支持库自动生成)

在你的 values/styles.xml

<style name="CardViewStyle.Light" parent="CardView.Light">
    <item name="android:foreground">?android:attr/selectableItemBackground</item>
</style>

<style name="CardViewStyle.Dark" parent="CardView.Dark">
    <item name="android:foreground">?android:attr/selectableItemBackground</item>
</style>

CardView 的布局

创建两种布局,一种用于深色,一种用于浅色。它们将包括这样的卡片视图:

<android.support.v7.widget.CardView
        android:id="@+id/my_card_view"
        style="@style/CardViewStyle.Light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:cardCornerRadius="2dp"
        android:elevation="2dp">

    ... other layout stuff ...

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

<android.support.v7.widget.CardView
        android:id="@+id/my_card_view"
        style="@style/CardViewStyle.Dark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:cardCornerRadius="2dp"
        android:elevation="2dp">

    ... other layout stuff ...

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

注意在每个 CardView 中切换样式。

其他自定义

如果您想自定义颜色而不仅仅是黑色或白色,您可以使用以下内容:

要获得 CardView drawable,您可以使用:

View cardView = findViewById(R.id.my_card_view);
Drawable cardViewDrawable cardView.getBackground();
convertIcon(setViewBackground(cardView, cardViewDrawable));

我的“convertIcon”如下所示:

/**
 *
 * @param drawable e.g. "getResources().getDrawable(R.drawable.my_drawable)"
 * @param tint e.g. "Color.parseColor(lightTitlebarFg)"
 * @return Drawable
 *
 */
public static Drawable convertIcon(Drawable drawable, int tint) {
    ColorFilter filter = new PorterDuffColorFilter(tint, PorterDuff.Mode.SRC_ATOP);
    drawable.setColorFilter(filter);
    return drawable;
}

我的“setViewBackground”如下:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("deprecation")
public static void setViewBackground(View view, Drawable drawable){
    if (UIUtils.isJB()) {
        view.setBackground(drawable);
    } else {
        view.setBackgroundDrawable(drawable);
    }
}

StateListDrawable

如果您想以编程方式创建 StateListDrawable 可以使用以下内容:

StateListDrawable states = new StateListDrawable();
states.addState(new int[] { android.R.attr.state_focused }, focusedDrawable);
states.addState(new int[] { android.R.attr.state_activated }, activatedDrawable);
states.addState(new int[] { android.R.attr.state_enabled }, defaultDrawable);
states.addState(new int[] {}, defaultDrawable);
于 2014-12-03T18:49:40.060 回答
3

这是我在存储在项目的可绘制文件夹中的 card_on_press.xml 文件中使用的代码:

<item>
    <shape>
        <padding
            android:bottom="0dp"
            android:left="0dp"
            android:right="0dp"
            android:top="0dp" />
        <solid android:color="@color/transparent" />
    </shape>
</item>

<item>
    <shape>
        <padding
            android:bottom="0dp"
            android:left="0dp"
            android:right="0dp"
            android:top="0dp" />
        <solid android:color="@color/card_shadow_1_on_press" />
        <corners android:radius="8dp" />
    </shape>
</item>
<item>
    <shape>
        <padding
            android:bottom="0dp"
            android:left="0dp"
            android:right="0dp"
            android:top="0dp" />
        <solid android:color="@color/card_shadow_2_on_press" />
        <corners android:radius="8dp" />
    </shape>
</item>
<!-- Background -->
<item>
    <shape>
        <solid android:color="@color/card_background_on_press" />
        <corners android:radius="8dp" />
    </shape>
</item>

于 2014-09-16T21:27:54.187 回答