在最新的 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>
使用以下代码修复:
值-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>
Note:
Drawable 默认值作为透明项就可以了,因为 CardView 为所有 Android 版本提供了默认背景。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)