Android - 在布局之间切换

时间:2011-09-13 16:06:51

标签: android

我有两种布局,当用户点击按钮时,在两种布局之间切换的最佳方法是什么?

4 个答案:

答案 0 :(得分:4)

您可以在按钮上点击<{p}}

答案 1 :(得分:1)

使用ViewSwitcher。

  1. 制作一个包含两个布局的布局文件。你的两个布局应放在viewswitcher中。

  2. 将使用按钮切换两个布局的onclick侦听器关联。

  3. 如果在不同文件中分隔两个布局,则可以在布局xml文件中使用标记。

答案 2 :(得分:1)

最好的方法是使用android.widget.ViewFlipper。有了它,您可以从xml创建不同的布局,然后使用这样的简单方法在它们之间切换:

   ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);

   // you can switch between next and previous layout and display it
   viewFlipper.showNext();
   viewFlipper.showPrevious();

  // or you can switch selecting the layout that you want to display
  viewFlipper.setDisplayedChild(1);
  viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.secondLayout)

带树布局的Xml示例:

     <ViewFlipper
            android:id="@+id/myViewFlipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/firstLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
               [...]
            </LinearLayout>

            <LinearLayout
                android:id="@+id/secondLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
               [...]
            </LinearLayout>

            <LinearLayout
                android:id="@+id/thirdLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
              [...]
            </LinearLayout>
      </ViewFlipper>

答案 3 :(得分:0)

在创建片段并在运行时将“布局”放入其中之后使用“片段管理器”或“查看寻呼机”,因为它还可以添加交换效果。不要使用setContentView(R.layout.your_layout)而不清除以前的布局(使用“消失”或“清除”)来改变运行时的布局,因为它会降低你的应用程序速度(因为现在有两个布局正在运行)甚至创建应用程序的混乱。