gpt4 book ai didi

android - 为旧版本的 Android 覆盖 Android-L CardView state_pressed

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:36:41 26 4
gpt4 key购买 nike

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

有谁知道我如何在新的 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>

最佳答案

使用以下代码修复:

values-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 之前的版本中提供替代动画,而不是大的蓝色/灰色方 block 。

这是我用作可绘制对象的 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 default 将作为透明项目很好,因为 CardView 为所有 android 版本提供了默认背景。

关于android - 为旧版本的 Android 覆盖 Android-L CardView state_pressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24518376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com