2017-10-16 65 views

回答

0

你不能提供一個以上的佈局的任何活動,但如果你想使用一個活動的不同佈局文件比你可以使用標籤包括所有的佈局文件到一個單一的佈局文件,並在你的活動中使用它們。

+0

會更好地利用碎片,而不是 –

+0

的確是這樣,你可以使用片段顯示在活動不同的看法,這是核心片段的概念,但如果你想在你的單身mainActivity所有視圖(不同的佈局視圖)的東西。 如果您的要求沒有同時顯示所有視圖,則可以使用該片段。它取決於UI使用片段的要求或者在xml中包含佈局。 – Mahavir

+0

我仍然不會說使用這種模式。想象一下,如果在一個活動中有10個佈局,則需要編寫多少行findviewby –

0

在主佈局包括您所有的佈局,使visibility GONE對於所有其他的佈局。基於按鈕點擊,您可以根據佈局ID顯示任何特定佈局。

示例XML是如下:

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

<!-- main layout for first time loading activity--> 
<LinearLayout 
    android:id="@+id/layout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

</LinearLayout> 

    <!-- Display layout based on button click--> 
<LinearLayout 
    android:visibility="gone" 
    android:id="@+id/layout2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

</LinearLayout> 

    <!-- Display layout based on button click--> 
<LinearLayout 
    android:visibility="gone" 
    android:id="@+id/layout3" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

</LinearLayout> 

    <!-- Display layout based on button click--> 
<LinearLayout 
    android:visibility="gone" 
    android:id="@+id/layout4" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

</LinearLayout> 

</LinearLayout> 
+0

內存消耗將是一個噩夢,這是一種理想的解決方案,儘管它是一種沒有額外的java文件而缺少佈局點的內存消耗 –

+0

@ Moshe Edri是的我同意內存消耗,但是我給出了基於他的需求的解決方案,因爲他不需要多一個java文件。 – Gaurav

0
<include layout="@layout/titlebar"/> 

標題欄是不同的佈局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/titlebar_bg" 
tools:showIn="@layout/activity_main" > 

<ImageView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/gafricalogo" /> 

包括在主要佈局這樣的..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/app_bg" 
android:gravity="center_horizontal"> 

<include layout="@layout/titlebar"/> 

<TextView android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" 
      android:padding="10dp" /></LinearLayout> 
0

您可以使用Fragments進行此操作。

簡單,在主活動佈局中聲明瞭一個FrameLayout裏。

併爲您希望每個佈局創建碎片。現在

,您可以切換使用段管理佈局。

請參閱此瞭解更多詳情:Here

希望它幫助!

0

不,你不能單活動使用多個setContentView()

如果你只是想其它XML佈局添加到另一個。你可以使用<include>標籤。

實施例:

layout_one.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="This is layout one"/> 
</LinearLayout> 

layout_two.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="This is layout two"/> 
</LinearLayout> 

activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent"> 

    <include layout="@layout/layout_one">/> 
    <include layout="@layout/layout_two">/> 
</LinearLayout> 
相關問題