2011-09-13 104 views
1

我有兩種佈局,當用戶單擊按鈕時,在兩種佈局之間切換的最佳方式是什麼?Android - 在佈局之間切換

+1

[佈局](HTTP之間,給予開關:/ /www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/) – Uttam

回答

1

使用ViewSwitcher。

  1. 使一個佈局文件包含兩個佈局。您的兩個佈局應放置在視頻切換器中。

  2. 將一個onclick監聽器與一個按鈕切換兩個佈局。

如果將不同文件中的兩個佈局分開,則可以在佈局xml文件中使用標記。

4

你可以創建片段,並把你的佈局圖,它在運行時或「查看傳呼機」,因爲它也可以添加交換作用後ButtonClick

0

使用「片段經理」打電話setContentView(R.layout.layout2)。不要使用setContentView(R.layout.your_layout)而不清除以前的佈局(使用「去掉」或「清除」),以便在運行時更改佈局,因爲它會降低應用程序的運行速度(因爲現在有兩個佈局運行),甚至會創建應用程序混淆。

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>