【问题标题】:Not getting Ripple effect on Android Lollipop CardView没有在 Android Lollipop CardView 上获得波纹效果
【发布时间】:2017-02-18 17:15:08
【问题描述】:

我试图在卡片视图中获得涟漪效果,我通过添加 android:background 属性来实现它,如 Android 开发者页面 https://developer.android.com/training/material/animations.html 中所述,但我没有获得涟漪效果,然后我更改了属性到https://stackoverflow.com/a/26975714/6866139 中给出的 android:foreground 仍然没有得到涟漪效应,这是我的 XML 代码

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@color/cardview_light_background"
        card_view:cardElevation="3dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:id="@+id/card_view"
        android:clickable="true"
        android:background="?android:attr/selectableItemBackground"
        android:layout_margin="@dimen/password_list_item_card_view_layout_margin">


            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="70dp">

                <TextView
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:id="@+id/logo_text_holder"
                    android:layout_marginTop="12dp"
                    android:layout_marginStart="12dp"
                    android:textColor="@color/colorPrimaryDark"
                    android:background="@drawable/circle"
                    android:paddingTop="8dp"
                    android:textSize="24sp"
                    android:textAlignment="center" />

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="8dp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/name_holder"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="24sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/email_holder"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="14sp"/>
                </LinearLayout>
            </LinearLayout>
    </android.support.v7.widget.CardView> 

有什么方法可以实现吗,请帮帮我 提前致谢

【问题讨论】:

  • 而不是 android:background="?android:attr/selectableItemBackground" 使用这个 android:foreground="?android:attr/selectableItemBackground"。
  • 我尝试同时使用 android:foreground 和 android:background 属性,但还是不行

标签: android android-5.0-lollipop


【解决方案1】:

我遇到了同样的问题,在 CardViewchildView 内,而不是 android:layout_width="match_parent" 使用 android:layout_width="wrap_content"

如下代码:

<android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/green"
            android:foreground="?attr/selectableItemBackground"
            android:background="?attr/selectableItemBackground"
            android:stateListAnimator="@animator/lift_on_touch"
            android:focusable="true"
            android:clickable="true"
            app:cardPreventCornerOverlap="false"
            app:cardUseCompatPadding="true">
        <TextView
            android:id="@+id/notification_details_med_close"
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:background="@color/green"
            android:text="CARDVIEW"
            android:textAppearance="?android:textAppearanceLarge"
            android:textColor="@android:color/white" />
        </android.support.v7.widget.CardView>

对于触摸时的提升动画,在animator-v21 文件夹中创建动画资源文件lift_on_touch

    <?xml version="1.0" encoding="utf-8"?>
<!-- animate the translationZ property of a view when pressed -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_pressed="true">
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="16dp"
                android:valueType="floatType"/>
        </set>
    </item>
    <item>
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="0"
                android:valueType="floatType"/>
        </set>
    </item>
</selector>

这将有助于 CardView 上的材料设计效果。

注意:android:stateListAnimator 适用于 API 21 及更高版本。

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2017-05-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 2017-05-12
    相关资源
    最近更新 更多