How to use Android ViewSwitcher?

前端 未结 4 903
自闭症患者
自闭症患者 2020-12-16 02:51

I\'m trying to use the ViewSwitcher to perform switches between two views. One is an ImageView and the other is a MapView. I have a button which the user clicks to perform t

相关标签:
4条回答
  • 2020-12-16 03:20

    Your ImageView tag should fall under ViewSwitcher tag, just like the com.google.android.maps.MapView.

    0 讨论(0)
  • 2020-12-16 03:30

    Are you setting Layout parameters at runtime or is everything in the XML?

    Have you tried wrapping the two views of the ViewSwitcher each into its own LinearLayout?

    0 讨论(0)
  • Add the ViewSwitcher widget to your xml layout file. to the ViewSwitcher add 2 new layouts.

    <ViewSwitcher
            android:id="@+id/viewSwitcher1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:inAnimation="@android:anim/slide_in_left" >
    
            <LinearLayout
            android:id="@+id/view1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/text"
                android:text="This is simplezdscsdc text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                </TextView>
    
            </LinearLayout>
    
    
        <LinearLayout
            android:id="@+id/view2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/text"
                android:text="This issdsdsds simplezdscsdc text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                </TextView>
    
            </LinearLayout>
        </ViewSwitcher>
    

    In your activity, add function to a button which switches between the views

    viewSwitcher =   (ViewSwitcher)findViewById(R.id.viewSwitcher1);
            myFirstView= findViewById(R.id.view1);
            mySecondView = findViewById(R.id.view2);
            button1 = (Button) findViewById(R.id.button1);
            button1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    if (viewSwitcher.getCurrentView() != myFirstView){
    
                        viewSwitcher.showPrevious(); 
                    } else if (viewSwitcher.getCurrentView() != mySecondView){
    
                        viewSwitcher.showNext();
                    }
                }
            });
    

    android:inAnimation="@android:anim/slide_in_left" allows the view to scroll in from the left

    0 讨论(0)
  • 2020-12-16 03:34

    You need to have a LinearLayout wrapping everything else. On the example I see more than one but no main one.

    0 讨论(0)
提交回复
热议问题