学习3
切换方法2:使用setContentView切换Layout
一个Activity可以对应多个Layout,可以根据需要在不同的时间显示不同的Layout来达到切换界面的目的。这样,不需要多个Activity就可以显示不同的界面,也不再需要在Activity间传送数据,变量可以直接引用。
如果使用setContentView(int layoutResID)方法切换Layout,在切换后再切换回,程序相当于重新显示一个界面,并非是把原来的界面隐藏后再显示。我们可以先用LayoutInflater把布局xml文件引入成View对象,再通过setContentView(View view)方法来切换视图。因为所有对View的修改都保存在View对象里,所以,当切换回原来的布局时,就可以直接显示原来修改后的样子。
例如有如下的两个Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/tv1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Some Message" />
<Button android:id="@+id/b1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="切换到layout2"/>
<Button android:id="@+id/b3"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="修改tv1"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/b2"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="切换到layout1"/>
</LinearLayout>
在一个Activity中使用这两个Layout的方法如下:
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
TextView tv1, tv2;
Button b1, b2,b3;
View layout1, layout2;
boolean view2Load = false;//main2是否载入过的flag
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(this);
layout1 = inflater.inflate(R.layout.layout1, null);
layout2 = inflater.inflate(R.layout.layout2, null);
setView1();
tv1 = (TextView) findViewById(R.id.tv1);
b1 = (Button) findViewById(R.id.b1);
b3 = (Button) findViewById(R.id.b3);
b3.setOnClickListener(l3);
b1.setOnClickListener(l1);
//控件及监听器只需一次查找绑定,切换view不影响
}
private void setView1() {
setContentView(layout1);
}
private void setView2() {
setContentView(layout2);
setView2Btn();
}
private void setView2Btn(){
if(!view2Load){ //如果首次显示layout2,查找控件并绑定监听器
b2 = (Button) findViewById(R.id.b2);
b2.setOnClickListener(l2);
view2Load=true;//flag设为true
}
}
OnClickListener l1 = new OnClickListener() {
@Override
public void onClick(View v) {
setView2();
}
};
OnClickListener l2 = new OnClickListener() {
@Override
public void onClick(View v) {
setView1();
}
};
OnClickListener l3 = new OnClickListener() {
@Override
public void onClick(View v) {
tv1.setText("Another Message");
//修改tv1的值,切换到layout2后再切换回来到layout1,可以发现tv1的值被保存了
}
};
}
Tips: android:layout_weight="1"可以在LinearLayout中表示某元素的权重,默认都为”0“。在默认情况下,如果屏幕排不下所有的元素,则屏幕外面的将会丢失。设置了权重值之后,系统将自动为各个元素分配其权重值对应的空间,数值大小表示其权重大小。
SplashForm的效果
有这两种切换方式,想要做到自动切换,达到SplashForm的效果也很简单了。以第一种切换方式为例,将Activity01画成SplashForm,AndroidManiefest.xml文件保证了Activity01是最先显示出来的。接着,将Activity01的实现代码改成:
public class Activity01 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 设置显示layout01.xml布局 */
setContentView(R.layout.layout01);
final int nWelcomeScreenDisplay = 3000; //默认3秒
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(Activity01.this, Activity02.class);
startActivity(intent);
Activity01.this.finish();
}
}, nWelcomeScreenDisplay);
}
}
即可在3秒之后,自动切换到Activity02显示。
使用切换Layout的方式来完成SplashForm与此类似,只是将切换Activity的代码改成切换Layout的代码即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义