2017-06-06 68 views
1

我是Android開發的新手,我試圖在cardview點擊上提升電梯效果。我在這個主題上找到了幾個教程 this one,this onethis answer on stackoverflow。 所有這些教程建議創建這樣CardView電梯觸摸

<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="6dp" 
       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> 

XML文件,然後把它放到RES /阿尼姆目錄。然而,當我試圖把這段代碼放在res/anim/lift_on_touch.xml中時,它給了我錯誤「元素選擇器必須聲明」,並建議將此文件移動到動畫器或可繪製目錄。我將它移動到animator-v21目錄中,並且沒有錯誤,但它不起作用。在這種情況下我該怎麼辦?

PS:我把clickable和stateListAnimator屬性添加到我的CardView中。這是我的CardView:

<android.support.v7.widget.CardView 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    app:cardElevation="2dp" 
    android:clickable="true" 
    android:stateListAnimator="@animator/lift_on_touch" 
    app:layout_constraintTop_toTopOf="@+id/top_guideline" 
    app:layout_constraintRight_toRightOf="@+id/right_guideline" 
    app:layout_constraintLeft_toLeftOf="@+id/left_guideline" 
    app:layout_constraintBottom_toBottomOf="@+id/inner_top_guideline" /> 
+0

這應該是一個非常簡單的解決辦法:你的XML複製到'RES/animator'目錄NOT res/anim,編譯並重新運行 –

+0

@ inner_class7我試過了,仍然沒有結果 –

+0

也刪除了objectAnimator附近的''標籤 –

回答

1

嘗試

<set> 
<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="6dp" 
       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> 
**</set>** 

它爲我這樣加<set>標籤周圍

+1

雖然這段代碼可以解決問題, t exp拉寧爲什麼或如何回答這個問題。請包含您的代碼的說明,因爲這有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因 –