如何使用Android视图切换器?

发布于 2024-09-30 23:31:00 字数 6903 浏览 6 评论 0原文

我正在尝试使用 ViewSwitcher 在两个视图之间执行切换。一个是 ImageView,另一个是 MapView。我有一个按钮,用户单击该按钮即可执行此切换,但我的代码导致错误。你们知道这是怎么回事吗?

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <relativelayout
        android:id="@+id/RelativeLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <imageview
            android:id="@+id/banner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignparenttop="true"
            android:background="@drawable/banner_location"
            android:clickable="false">
        </imageview>

        <linearlayout
            android:id="@+id/LinearLayout01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/banner"
            android:layout_centerhorizontal="true"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
            android:paddingbottom="5px">

            <imagebutton
                android:id="@+id/btnBusSvcs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginright="5px"
                android:layout_margintop="5px"
                android:background="@drawable/btn_bus_svcs">
            </imagebutton>

            <imagebutton
                android:id="@+id/btnDrvRoute"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginleft="5px"
                android:layout_margintop="5px"
                android:background="@drawable/btn_drv_route">
            </imagebutton>
        </linearlayout>

        <viewswitcher
            android:id="@+id/switcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imgViewBusSvcs"
            android:layout_below="@+id/LinearLayout01">

            <imageview
                android:id="@+id/imgViewBusSvcs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/LinearLayout01"
                android:layout_centerinparent="true"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/bus_svcs"
                android:clickable="false">
            </imageview>

            <com.google.android.maps.mapview
                android:id="@+id/mapview"
                android:layout_width="fill_parent"
                android:layout_height="300px"
                android:apikey="REMOVE MY API KEY"
                android:clickable="true">
            </com.google.android.maps.mapview>
        </viewswitcher>

        <imagebutton
            android:id="@+id/btnBackHome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignparentbottom="true"
            android:layout_centerhorizontal="true"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/btn_back_to_home">
        </imagebutton>
    </relativelayout>
</linearlayout>

错误是这样的

11-05 20:53:13.964: ERROR/AndroidRuntime(1417): Uncaught handler: thread main exiting due to uncaught exception
11-05 20:53:13.995: ERROR/AndroidRuntime(1417): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.RelativeLayout.getRelatedView(RelativeLayout.java:694)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.RelativeLayout.getRelatedViewParams(RelativeLayout.java:708)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.RelativeLayout.applySizeRules(RelativeLayout.java:629)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:279)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.View.measure(View.java:7115)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.View.measure(View.java:7115)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.View.measure(View.java:7115)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.View.measure(View.java:7115)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewRoot.performTraversals(ViewRoot.java:698)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.os.Looper.loop(Looper.java:123)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.app.ActivityThread.main(ActivityThread.java:3948)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at java.lang.reflect.Method.invoke(Method.java:521)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at dalvik.system.NativeStart.main(Native Method)

非常感谢您的帮助! :)

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 this switch but my codes are causing an error. Do you guys have any idea what's about?

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <relativelayout
        android:id="@+id/RelativeLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <imageview
            android:id="@+id/banner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignparenttop="true"
            android:background="@drawable/banner_location"
            android:clickable="false">
        </imageview>

        <linearlayout
            android:id="@+id/LinearLayout01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/banner"
            android:layout_centerhorizontal="true"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
            android:paddingbottom="5px">

            <imagebutton
                android:id="@+id/btnBusSvcs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginright="5px"
                android:layout_margintop="5px"
                android:background="@drawable/btn_bus_svcs">
            </imagebutton>

            <imagebutton
                android:id="@+id/btnDrvRoute"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginleft="5px"
                android:layout_margintop="5px"
                android:background="@drawable/btn_drv_route">
            </imagebutton>
        </linearlayout>

        <viewswitcher
            android:id="@+id/switcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imgViewBusSvcs"
            android:layout_below="@+id/LinearLayout01">

            <imageview
                android:id="@+id/imgViewBusSvcs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/LinearLayout01"
                android:layout_centerinparent="true"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/bus_svcs"
                android:clickable="false">
            </imageview>

            <com.google.android.maps.mapview
                android:id="@+id/mapview"
                android:layout_width="fill_parent"
                android:layout_height="300px"
                android:apikey="REMOVE MY API KEY"
                android:clickable="true">
            </com.google.android.maps.mapview>
        </viewswitcher>

        <imagebutton
            android:id="@+id/btnBackHome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignparentbottom="true"
            android:layout_centerhorizontal="true"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/btn_back_to_home">
        </imagebutton>
    </relativelayout>
</linearlayout>

The error is as such

11-05 20:53:13.964: ERROR/AndroidRuntime(1417): Uncaught handler: thread main exiting due to uncaught exception
11-05 20:53:13.995: ERROR/AndroidRuntime(1417): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.RelativeLayout.getRelatedView(RelativeLayout.java:694)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.RelativeLayout.getRelatedViewParams(RelativeLayout.java:708)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.RelativeLayout.applySizeRules(RelativeLayout.java:629)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:279)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.View.measure(View.java:7115)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.View.measure(View.java:7115)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.View.measure(View.java:7115)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:2875)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.View.measure(View.java:7115)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewRoot.performTraversals(ViewRoot.java:698)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.os.Looper.loop(Looper.java:123)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at android.app.ActivityThread.main(ActivityThread.java:3948)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at java.lang.reflect.Method.invoke(Method.java:521)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-05 20:53:13.995: ERROR/AndroidRuntime(1417):     at dalvik.system.NativeStart.main(Native Method)

Really appreciate the help! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喵星人汪星人 2024-10-07 23:31:00

将 ViewSwitcher 小部件添加到您的 xml 布局文件中。
向 ViewSwitcher 添加 2 个新布局。

<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" />
            
    </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" />
                        
    </LinearLayout>

</ViewSwitcher>

在您的 Activity 中,向在视图之间切换的按钮添加功能

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) {
        if (viewSwitcher.getCurrentView() != myFirstView) {
            viewSwitcher.showPrevious(); 
        } else if (viewSwitcher.getCurrentView() != mySecondView) {                 
            viewSwitcher.showNext();
        }
    }
});

android:inAnimation="@android:anim/slide_in_left" 允许视图从左侧滚动。

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" />
            
    </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" />
                        
    </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) {
        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.

溇涏 2024-10-07 23:31:00

您需要有一个 LinearLayout 来包装其他所有内容。在这个例子中,我看到不止一个,但没有主要的。

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

故事↓在人 2024-10-07 23:31:00

您是在运行时设置布局参数还是所有内容都在 XML 中?

您是否尝试过将 ViewSwitcher 的两个视图分别包装到自己的 LinearLayout 中?

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?

折戟 2024-10-07 23:31:00

你的 ImageView 标签应该属于 ViewSwitcher 标签,就像 com.google.android.maps.MapView 一样。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文