2017-02-18 158 views
1

我試圖讓卡圖的漣漪效應沒有得到波及效應,我實現它加入了android:作爲Android開發者https://developer.android.com/training/material/animations.html頁面描述背景屬性,但我沒有得到的連鎖反應,然後我改變了屬性的android:前景爲https://stackoverflow.com/a/26975714/6866139給我仍然在這裏不是獲得連鎖反應是我的XML代碼在Android棒棒糖CardView

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

有什麼辦法來實現它,請幫我提前

謝謝
+0

相反的android:背景= 「機器人:ATTR/selectableItemBackground」 使用這個機器人:前景= 「機器人:ATTR/selectableItemBackground」。 –

+0

我嘗試使用android:foreground和android:background屬性,但仍然無法正常工作 – RikudouSennin

回答

0

我有同樣的問題,在的CardView 210,而不是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文件夾

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

此創建一個動畫資源文件lift_on_touch將有助於CardView的材料設計效果。

注:android:stateListAnimator作品與API 21以上。