覆盖的Andr​​oid-L CardView STATE_ pressed对于旧版本的Andr​​oid系统 [英] Override Android-L CardView state_pressed for Older versions of Android

查看:181
本文介绍了覆盖的Andr​​oid-L CardView STATE_ pressed对于旧版本的Andr​​oid系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的Andr​​oid SDK,我们现在有了新的CardView,我已经取代我的旧CustomCardView新版本,但这种在旧版本的Andr​​oid上运行时我看到 STATE_ pressed &放大器; state_focused 是显示了CardView上述丑陋的广场......

没有人知道我怎么能效仿以下新CardView而只是采用了Android的老版本的时候?

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:enterFadeDuration =150
    机器人:exitFadeDuration =150>

    <项目的android:STATE_ pressed =真
          机器人:可绘制=@可绘制/ card_on_ preSS/>
    <项目安卓state_focused =真正的机器人:state_enabled =真
          机器人:可绘制=@可绘制/ card_on_focus/>
    <项目安卓state_enabled =真
          机器人:可绘制=@可绘制/ card_default/>
    <项目安卓state_enabled =假
          机器人:可绘制=@可绘制/ card_on_ preSS/>

< /选择器>
 

而对于那些你有兴趣在这里就是我现在使用的CardView:

 < android.support.v7.widget.CardView
    的xmlns:card_view =htt​​p://schemas.android.com/apk/res-auto
    机器人:ID =@ + ID / CardView
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginLeft =10dp
    机器人:layout_marginRight =10dp
    机器人:layout_marginTop =10dp
    机器人:前景=机器人:ATTR / selectableItemBackground
    机器人:的onClick =RunSomeMethod
    card_view:cardCornerRadius =4DP
    机器人:可聚焦=真
    机器人:海拔=2DP>

    <的LinearLayout
        机器人:ID =@ + ID / LinearLayout中
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=横向
        机器人:可聚焦=假
        机器人:能见度=有形与GT;

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

解决方案

修正了以下code:

值-V21 \ styles.xml:

 <样式名称=CardViewStyle父=CardView.Light>
    <项目名称=机器人:前台?>机器人:ATTR / selectableItemBackground< /项目>
< /风格>
 

值\ styles.xml:

 <样式名称=CardViewStyle父=机器人:Widget.TextView>
    <项目名称=机器人:前台> @可绘制/卡< /项目>
< /风格>
 

从布局\ main_layout.xml A卡:

 < android.support.v7.widget.CardView
    的xmlns:card_view =htt​​p://schemas.android.com/apk/res-auto
    机器人:ID =@ + ID / Card_View
    风格=@风格/ CardViewStyle
    机器人:layout_width =480dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =center_horizo​​ntal
    机器人:layout_marginLeft =10dp
    机器人:layout_marginRight =10dp
    机器人:layout_marginTop =10dp
    card_view:cardCornerRadius =4DP
    机器人:海拔=2DP>
 

这将允许您提供的V21 +动画,但也提供在pre-V21另类的动画,而不是大蓝/灰色正方形。

下面是我使用的是作为绘制对于那些你感兴趣的card.xml:

 <选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:enterFadeDuration =150
    机器人:exitFadeDuration =150>

    <项目的android:STATE_ pressed =真
          机器人:可绘制=@可绘制/ card_on_ preSS/>
    <项目安卓state_focused =真正的机器人:state_enabled =真
          机器人:可绘制=@可绘制/ card_on_focus/>
    <项目安卓state_enabled =真
          机器人:可绘制=@可绘制/ card_default/>
    <项目安卓state_enabled =假
          机器人:可绘制=@可绘制/ card_on_ preSS/>
< /选择器>
 

注意: 绘制对象默认将罚款作为一种透明的项目作为CardView提供了一个默认的背景为所有的Andr​​oid版本。

In the latest Android SDK we now have the new CardView, I have replaced my old CustomCardView with the new version, but when running with this on older versions of Android I see that the state_pressed & state_focused are ugly squares which show up above the CardView...

does anyone know how I could emulate the following in the new CardView but only when using older versions of 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>

And for those of you interested here is the CardView that I am using now:

<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>

解决方案

Fixed with the following code:

values-v21\styles.xml:

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

values\styles.xml:

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

A card from 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">

This will allow you to offer the animations in v21+ but also offer alternative animations in pre-v21 rather than the big blue/grey square.

Here is the card.xml I'm using as a drawable for those of you interested:

<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 default will be fine as a transparent item as CardView provides a default background for all android versions.

这篇关于覆盖的Andr​​oid-L CardView STATE_ pressed对于旧版本的Andr​​oid系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆