一个Activity分别加载多个layout布局文件
来源:互联网 发布:java面向过程实现加法 编辑:程序博客网 时间:2024/06/05 21:11
本程序只有一个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; } } }
真机测试如下:
阅读全文
0 1
- 一个Activity分别加载多个layout布局文件
- 在一个layout上加载一个activity
- 一个旋转layout布局文件
- 在一个Activity中切换多个layout
- 不同的布局文件怎么加载到一个activity中
- 为同一界面建立多个layout分辨率布局文件
- 多个Activity同用一个布局或控件
- 一个ListView使用多个布局文件
- Activity是如何加载布局文件的?
- Android多分辨率布局,多个layout
- android 多个layout布局listView
- 自定义adapter 无法加载layout中布局文件的原因
- [yii2]不加载layout布局文件的实现方法
- Framelayout布局中嵌套多个布局layout的显示
- Layout 布局文件
- 把多个CSV文件加载到一个EXCEL文件
- 第二十一天 Activity的生命周期、显式启动第二个界面、四大布局(layout)
- android Activity.this.getLayoutInflater()::动态加载布局文件,实现弹窗效
- http://blog.csdn.net/dragonxiangfu/article/details/14120785
- Gulp的安装与配置
- 刚上线的“新睿云”到底如何达到高可用?IOPS高达200000?
- 【云隐】STM32F103C8T6实现串口IAP方式升级固件
- HTTP协议:概括性知识
- 一个Activity分别加载多个layout布局文件
- 秒懂Vuejs、Angular、React原理和前端发展历史
- 人是一种复杂的动物
- java 循环中String与StringBuilder效率探究
- dijkstra的多解的情况
- Nexus私服使Maven更加强大
- keystore和重签名
- [知了堂学习笔记]_JavaScript之数据类型
- python--读取文件夹名称 import os