2016-07-17 40 views
0

比方說,我有這個結構的XML:是否有快速切換相對佈局的方法?

FrameLayout 
    RelativeLayout (on whole screen) 
     Buttons, Texts etc. 
    RelativeLayout (on whole screen) 
     Buttons, Texts etc. 

在「設計」或「預覽」選項卡中,我總是看到我創建的第一個相對佈局。我一直在通過讓另一個不可見來切換它們,但是當你有兩個以上的佈局時,它是一團糟。 Android中有沒有提供此功能的選項,我是指在一個XML中切換不同的佈局?

+0

片段?您使用FragmentTransaction替換整個佈局 –

+0

我不是代碼明智的,我只需要在設計選項卡上的佈局之間切換。 –

+0

點擊shift鍵兩次...鍵入要打開的XML文件的名稱? –

回答

1

在Android Studio中,您不能在同一時間正確地查看兩個RelativeLayouts如果它們都填滿你的屏幕。我建議你使用兩個包含RelativeLayout的xml來設計它們以滿足你的需求。之後,您可以輕鬆地將它們合併到一個xml中。例如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <include android:layout_width="fill_parent" android:layout_height="fill_parent" layout="@layout/your_first_relativelayout" /> 
    <include android:layout_width="fill_parent" android:layout_height="fill_parent" layout="@layout/your_second_relativelayout" /> 
</FrameLayout> 

如果要隱藏或顯示programmaticly一個佈局時,應同時將與您RelativeLayouts一個唯一的ID,這樣的:

your_first_relativelayout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/your_relative_layout_id" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     .... 
    </RelativeLayout> 

要顯示它:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.your_relative_layout_id); 
if (rl != null) { 
    rl.setVisibility(View.VISIBLE); 
} 

並隱藏它:

rl.setVisibility(View.GONE); 
0

我覺得沒有選擇。
最好使用兩個佈局文件並在編輯器中將它們切換。
(和兩個佈局文件將包括通過使用<include>標籤形成原始佈局文件)