2017-10-16 85 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> 
相关问题