How to use Android ViewSwitcher?

谁说胖子不能爱 提交于 2019-11-29 02:07:49
Vincent Mimoun-Prat

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?

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

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

user1094717

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!