2012-04-02 85 views
1

在试图简化我对于比赛我开发的UI代码,我觉得很值得去尝试传递的RelativeLayout通过的onClick()的的ImageButton的。RelativeLayout可以通过onClick传递吗?

我在想,如果这是可能的,因为它似乎是强制关闭每当ImageButton的点击?这是我的代码的一般问题,还是通过RelativeLayout传递的实际机制不可能?

在先进的感谢, 这里是我的代码:

public class SecondaryMenu extends Activity { 

    ImageButton scrambledbutton; //Button That checks click and performs animation for the scrambled carton. 
    ImageButton breakfastburritobutton; //Button for animation for breakfast burrito carton. 
    ImageButton eggsbenedictbutton; //Button for eggs benedict carton animation. 
    ImageButton eggsontoastbutton; //Button for eggs on toast animation 
    ImageButton eggsaladsandwichbutton; //Button for egg salad sandwich animation. 
    ImageButton eastereggsbutton; //Button for easter eggs animation. 
    ImageButton ostricheggsbutton; //Button for ostrich eggs animation. 
    ImageButton quaileggsbutton; //Button for quail eggs animation. 
    RelativeLayout animatedcarton; //RelativeLayout for animating 

    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.secondary_menu); //Sets the current layout to the Secondary_Menu layout 


      //Does animation based on button press - SCRAMBLED 
      scrambledbutton = (ImageButton) findViewById(R.id.scrambledbutton); 
      scrambledbutton.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) {    

        //Calls the animation for the carton, and passes it the view to animate. 
        cartonanimation(findViewById (R.id.scrambledcarton)); 
       } 
      }); 

      //Does animation based on button press - BREAKFAST BURRITO 
      breakfastburritobutton = (ImageButton) findViewById (R.id.breakfastburritobutton); 
      breakfastburritobutton.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        //Calls animation and passes view to be animated. 
        cartonanimation(findViewById (R.id.breakfastburittocarton));     
       } 
      }); 

    } 

    //Animation that slides clicked carton off the screen 
    private void cartonanimation(View tempview) { 
     animatedcarton = (RelativeLayout) tempview; 
     animatedcarton.setVisibility(RelativeLayout.VISIBLE); 
     animatedcarton.setBackgroundResource(R.anim.secondarymenuanimation); 
     AnimationDrawable viewAnimation = (AnimationDrawable) animatedcarton.getBackground(); 
     viewAnimation.start(); 
     viewAnimation.setOneShot(true); 
    } 
} 

而且我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="320dp" 
android:layout_height="wrap_content" 
android:gravity="top"> 

<!-- Carton number 1 - Scrambled --> 
<RelativeLayout 
    android:orientation="vertical" 
    android:layout_width="320dp" 
    android:layout_height="160dp" 
    android:gravity="top" 
    android:id="@+id/scrambledcarton"> 
    <ImageButton 
     android:id="@+id/scrambledbutton" 
     android:background="@drawable/cartonbackground" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginBottom="20dp"> 
    </ImageButton> 
    <TextView 
     style="@style/cartondishstyle" 
     android:text="@string/carton1"><!-- Change for each Carton --> 
    </TextView> 
    <TextView 
     style="@style/cartoneggcountstyle" 
     android:text="18" 
     android:layout_marginRight="40dp"><!-- Adjust for each Carton --> 
    </TextView> 
</RelativeLayout> 

<!-- Carton number 2 - Breakfast Burrito --> 
<RelativeLayout 
    android:orientation="vertical" 
    android:layout_width="320dp" 
    android:layout_height="160dp" 
    android:gravity="top" 
    android:id="@+id/breakfastburittocarton"> 
    <ImageButton 
     android:id="@+id/breakfastburritobutton" 
     android:background="@drawable/cartonbackground" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginBottom="20dp"> 
    </ImageButton> 
    <TextView 
     style="@style/cartondishstyle" 
     android:text="@string/carton2"><!-- Change for each Carton --> 
    </TextView> 
    <TextView 
     style="@style/cartoneggcountstyle" 
     android:text="18" 
     android:layout_marginRight="40dp"><!-- Adjust for each Carton --> 
    </TextView> 
</RelativeLayout> 

</LinearLayout> 

和日志猫

04-02 00:27:24.725: ERROR/AndroidRuntime(1674): FATAL EXCEPTION: main 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674): android.content.res.Resources$NotFoundException: File res/anim/secondarymenuanimation.xml from drawable resource ID #0x7f040000 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.content.res.Resources.loadDrawable(Resources.java:1697) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.content.res.Resources.getDrawable(Resources.java:581) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.view.View.setBackgroundResource(View.java:7533) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at com.example.eggRoll.SecondaryMenu.cartonanimation(SecondaryMenu.java:54) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at com.example.eggRoll.SecondaryMenu.access$0(SecondaryMenu.java:51) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at com.example.eggRoll.SecondaryMenu$1.onClick(SecondaryMenu.java:35) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.view.View.performClick(View.java:2485) 
04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.view.View$PerformClick.run(View.java:9080) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.os.Handler.handleCallback(Handler.java:587) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.os.Handler.dispatchMessage(Handler.java:92) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.os.Looper.loop(Looper.java:123) 
04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.app.ActivityThread.main(ActivityThread.java:3683) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at java.lang.reflect.Method.invoke(Method.java:507) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at dalvik.system.NativeStart.main(Native Method) 
04-02 00:27:24.725: ERROR/AndroidRuntime(1674): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag set 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:783) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  at android.content.res.Resources.loadDrawable(Resources.java:1694) 

04-02 00:27:24.725: ERROR/AndroidRuntime(1674):  ... 16 more 
+0

请添加任何的logcat输出,可能是进一步的描述,你的原职。然而,为了解决您的问题有关,如果你是“能够”通过的onClick传递一个RelativeLayout的 - 是的,你可以,你可以通过几乎来自View延伸的任何*查看。 :-) – ninetwozero 2012-04-02 01:31:03

+0

补充,希望它帮助了 – Rogue 2012-04-02 03:14:53

+0

邮政secondarymenuanimation文件 – 2012-04-02 03:47:10

回答

0

你可能要忍受viewAnimation.setOneShot(true);viewAnimation.start();,因为它会影响它的播放方式。

+0

试过了,它没有,我可以看到,它仍然强行关闭任何影响。 – Rogue 2012-04-02 03:18:28

相关问题