Android - 查看切换器

时间:2011-09-13 18:45:19

标签: android

我想动态添加视图切换器。

  ViewSwitcher switcher = new ViewSwitcher(this);

如何将我的两个布局添加到视图切换器并使用代码在视图之间切换?

错误:

A1 [Android Application]    
DalvikVM[localhost:8604] (may be out of synch)  
    Thread [<1> main] (Suspended (exception RuntimeException))  
        ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1815    
        ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1831 
        ActivityThread.access$500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 122    
        ActivityThread$H.handleMessage(Message) line: 1024  
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 132 
        ActivityThread.main(String[]) line: 4123    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 491  
        ZygoteInit$MethodAndArgsCaller.run() line: 841  
        ZygoteInit.main(String[]) line: 599 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<8> Binder Thread #2] (Running) (may be out of synch)   
    Thread [<7> Binder Thread #1] (Running) (may be out of synch)   
    Thread [<9> SoundPool] (Running) (may be out of synch)  
    Thread [<10> SoundPoolThread] (Running) (may be out of synch)   

2 个答案:

答案 0 :(得分:0)

http://developer.android.com/reference/android/widget/ViewSwitcher.html

公共方法

void     addView(View child, int index, ViewGroup.LayoutParams params)

添加具有指定布局参数的子视图。

答案 1 :(得分:0)

参考ViewSwitcher的{​​{3}}。您可以根据需要使用addView()showNext()方法。

使用索引0和1调用addView两次。

addView(child, index, 
           new ViewGroup.LayoutParams(
                     LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

这应该没有问题..