2014-12-02 89 views
0

我有一個單選按鈕組包含兩個單選按鈕:動畫單選按鈕

<RadioGroup 
    android:id="@+id/radios" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:inputType="text" 
    android:orientation="vertical" > 

    <RadioButton 
     android:id="@+id/first" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:button="@null" 
     android:drawablePadding="30dp" 
     android:drawableRight="@drawable/selector_radio" 
     android:text="2 times" 
     android:textSize="20sp" 
     android:layout_marginLeft="20dp" /> 

    <RadioButton 
     android:id="@+id/second" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:button="@null" 
     android:drawablePadding="30dp" 
     android:drawableRight="@drawable/selector_radio" 
     android:text="3 times" 
     android:textSize="20sp" 
     android:layout_marginLeft="20dp" /> 
</RadioGroup> 

現在我想動畫添加到單選按鈕。我有兩個動畫單選按鈕 的每個狀態。例如: animation_radio_off:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 
     <item 
      android:drawable="@drawable/btn_off_00" 
      android:duration="10"/> 
     <item 
      android:drawable="@drawable/btn_off_00" 
      android:duration="10"/> 
...... 
    </animation-list> 

animation_radio_off就像animation_radio_on只是改變圖片。 我的選擇:

selector_radio.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item 
     android:drawable="@drawable/animation_radio_on" 
     android:state_checked="true" /> 
    <item 
     android:drawable="@drawable/animation_radio_off" 
     android:state_checked="false"/> 

</selector> 

但還有一些錯誤happend和我的應用程序無法運行。 我花了很多時間來搜索關於單選按鈕的動畫。我也參考這個鏈接:Android Radio Button Animation

但我gusse它不適合我的情況。 你對我的問題有任何想法嗎?謝謝。 編輯:我的日誌貓:

12-02 17:40:18.155: E/AndroidRuntime(25421): FATAL EXCEPTION: main 
12-02 17:40:18.155: E/AndroidRuntime(25421): Process: com.example.knock, PID: 25421 
12-02 17:40:18.155: E/AndroidRuntime(25421): android.view.InflateException: Binary XML file line #14: Error inflating class android.widget.RadioButton 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.LayoutInflater.createView(LayoutInflater.java:640) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.LayoutInflater.rInflate(LayoutInflater.java:821) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.LayoutInflater.inflate(LayoutInflater.java:511) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:428) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.app.Dialog.setContentView(Dialog.java:580) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at com.example.knock.SettingActivity.onClick(SettingActivity.java:81) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.View.performClick(View.java:5162) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.View$PerformClick.run(View.java:20873) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.os.Handler.handleCallback(Handler.java:739) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.os.Handler.dispatchMessage(Handler.java:95) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.os.Looper.loop(Looper.java:145) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.app.ActivityThread.main(ActivityThread.java:5834) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at java.lang.reflect.Method.invoke(Native Method) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at java.lang.reflect.Method.invoke(Method.java:372) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) 
12-02 17:40:18.155: E/AndroidRuntime(25421): Caused by: java.lang.reflect.InvocationTargetException 
12-02 17:40:18.155: E/AndroidRuntime(25421): at java.lang.reflect.Constructor.newInstance(Native Method) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
12-02 17:40:18.155: E/AndroidRuntime(25421): ... 21 more 
12-02 17:40:18.155: E/AndroidRuntime(25421): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/selector_radio.xml from drawable resource ID #0x7f020069 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.content.res.Resources.loadDrawableForCookie(Resources.java:3707) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.content.res.Resources.loadDrawable(Resources.java:3575) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.content.res.TypedArray.getDrawable(TypedArray.java:750) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.widget.TextView.<init>(TextView.java:1142) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.widget.Button.<init>(Button.java:115) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.widget.CompoundButton.<init>(CompoundButton.java:84) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.widget.RadioButton.<init>(RadioButton.java:65) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.widget.RadioButton.<init>(RadioButton.java:61) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.widget.RadioButton.<init>(RadioButton.java:57) 
12-02 17:40:18.155: E/AndroidRuntime(25421): ... 24 more 
12-02 17:40:18.155: E/AndroidRuntime(25421): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'duration' attribute 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:308) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1150) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1063) 
12-02 17:40:18.155: E/AndroidRuntime(25421): at android.content.res.Resources.loadDrawableForCookie(Resources.java:3691) 
+0

Selector_radio.xml應該是'Selector'而不是'Animation-list',對吧? – zozelfelfo 2014-12-02 11:11:52

+0

哦。抱歉。我錯了。我將它改爲選擇器標籤。但兩個單選按鈕總是以真實狀態顯示圖像。我不明白這種情況。 – ducanhZ5Z 2014-12-03 00:44:06

回答

0

我會問一個logcat的,但是從我的頭頂,你在佈局設置按鈕本身爲空,然後你(可能)指示資源經理分配給你的動畫,所以也許你的問題在那裏。

我看不到您將動畫分配給單選按鈕的位置(您可能在代碼中執行此操作)。

Logcat和代碼將有助於揭示這一點。

+0

我加了我的日誌貓。並遵循自定義廣播的例子,我認爲android:drawableRight =「@ drawable/selector_radio」是爲單選按鈕分配動畫。 – ducanhZ5Z 2014-12-02 10:42:54

+0

從logcat我可以看到,在selector_radio.xml動畫沒有「持續時間」屬性設置。 – 2014-12-02 10:50:13

+0

tks回覆。我編輯了我的問題。在selector_radio.xml中是seletor標籤,不包含「duration」 – ducanhZ5Z 2014-12-03 00:45:42