一個Activity分別載入多個layout佈局檔案
阿新 • • 發佈:2019-02-17
本程式只有一個Activity,首先載入activity_main佈局檔案。通過點選不同的按鈕,分別載入不同的佈局檔案layout_1、layout_2、layout_3。
activity_main.xml檔案如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.horrorkwangmail.a20171208_multilayout.ActivityMain"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="This is Layout main" android:textAllCaps="false" android:textSize="25sp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"/> <Button android:id="@+id/buttonLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:text="Layout 1" android:textAllCaps="false" app:layout_constraintEnd_toStartOf="@+id/buttonLayout2" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" android:onClick="buttonClick"/> <Button android:id="@+id/buttonLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:text="Layout 2" android:textAllCaps="false" app:layout_constraintBottom_toBottomOf="@+id/buttonLayout1" app:layout_constraintEnd_toStartOf="@+id/buttonLayout3" app:layout_constraintStart_toEndOf="@+id/buttonLayout1" app:layout_constraintTop_toTopOf="@+id/buttonLayout1" android:onClick="buttonClick"/> <Button android:id="@+id/buttonLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Layout 3" android:textAllCaps="false" app:layout_constraintBottom_toBottomOf="@+id/buttonLayout2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/buttonLayout2" app:layout_constraintTop_toTopOf="@+id/buttonLayout2" android:onClick="buttonClick"/> </android.support.constraint.ConstraintLayout>
layout_1.xml檔案如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/buttonBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="Back to Layout main" android:textAllCaps="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="30dp" android:text="This is Layout 1" android:textSize="25sp" android:textAllCaps="false" app:layout_constraintBottom_toTopOf="@+id/buttonBack" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout>
layout_2.xml檔案如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/buttonBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="Back to Layout main" android:textAllCaps="false" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="30dp" android:text="This is Layout 2" android:textSize="25sp" android:textAllCaps="false" app:layout_constraintBottom_toTopOf="@+id/buttonBack" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout>
layout_3.xml檔案如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Back to Layout main"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:text="This is Layout 3"
android:textSize="25sp"
android:textAllCaps="false"
app:layout_constraintBottom_toTopOf="@+id/buttonBack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
strings.xml檔案如下:
<resources>
<string name="app_name">MultiLayout</string>
<string name="layoutMain">Layout main</string>
<string name="layout1">Layout 1</string>
<string name="layout2">Layout 2</string>
<string name="layout3">Layout 3</string>
</resources>
ActivityMain.java檔案如下:
package com.horrorkwangmail.a20171208_multilayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ActivityMain extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //首先載入activity_main佈局檔案
this.setTitle(R.string.layoutMain); //設定標題欄名稱
}
public void buttonClick(View v)
{
switch (v.getId())
{
case R.id.buttonLayout1:
setContentView(R.layout.layout_1); //載入layout_1佈局檔案
this.setTitle(R.string.layout1); //設定標題欄名稱
//點選按鈕,回到activity_main佈局檔案
//並設定標題欄名稱
Button buttonBack1=findViewById(R.id.buttonBack);
buttonBack1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
setContentView(R.layout.activity_main);
setTitle(R.string.layoutMain);
}
});
break;
case R.id.buttonLayout2:
setContentView(R.layout.layout_2); //載入layout_2佈局檔案
this.setTitle(R.string.layout2); //設定標題欄名稱
//點選按鈕,回到activity_main佈局檔案
//並設定標題欄名稱
Button buttonBack2=findViewById(R.id.buttonBack);
buttonBack2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
setContentView(R.layout.activity_main);
setTitle(R.string.layoutMain);
}
});
break;
case R.id.buttonLayout3:
setContentView(R.layout.layout_3); //載入layout_3佈局檔案
this.setTitle(R.string.layout3); //設定標題欄名稱
//點選按鈕,回到activity_main佈局檔案
//並設定標題欄名稱
Button buttonBack3=findViewById(R.id.buttonBack);
buttonBack3.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
setContentView(R.layout.activity_main);
setTitle(R.string.layoutMain);
}
});
break;
default:
break;
}
}
}
真機測試如下: