手机页面的转换----setContentView的应用
2010-12-15 13:43
351 查看
实现手机页面的转换,最简单的方式就是改变Activity的Layout!在这个例子里,将布局两个Layout,分别为Layout1(main.xml)与Layout2(mylayout.xml),默认载入的Layout为main.xml,且在Layout1当中创建一个按钮,当点击按钮时,显示第二个layout(mylayout.xml);同样的,在Layout2里设计一个按钮,当点击第二个Layout的按钮之后,则显示回到原来的Layout1。
运行结果:
![](http://hi.csdn.net/attachment/201012/15/0_1292391815Cy7y.gif)
程序:
1、ex03_9/src/com.example.ex03_09/ex03-09.java
在主程序中预加载的Layout是main.xml,屏幕上显示的是黑色背景的“This is Layout 1 !”,在第一个layout上的按钮被点击的同时,改变Activity的layout为mylayout.xml,屏幕上显示变成白色背景的“This is layout 2 !”,并利用Button点击时,调用方法的不同做两个layout之间的切换。
package com.ezample.ex03_09;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ex03_09 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*以findViewById()取得Button对象,并添加onClickListener*/
Button b1=(Button)findViewById(R.id.Button01);
b1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
jumpToLayout2();
}
});
}
/*method jumpToLayout2:将layout由main.xml切换成mylayout.xml*/
public void jumpToLayout2()
{
/*将layout改成mylayout.xml*/
setContentView(R.layout.mylayout);
/*以findViewById()取得Button对象,并添加onClickListener*/
Button b2=(Button)findViewById(R.id.Button02);
b2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
jumpToLayout1();
}
});
}
/*method jumpToLayout1:将layout由mylayout.xml切换成main.xml*/
public void jumpToLayout1()
{
/*将layout改成mylayout.xml*/
setContentView(R.layout.main);
/*以findViewById()取得Button对象,并添加onClickListener*/
Button b1=(Button)findViewById(R.id.Button01);
b1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
jumpToLayout2();
}
});
}
}
2、ex03_9/res/layout/main.xml
为了突出layout间切换的效果,特别改变了两个个layout的背景色以及输出文字。在main.xml中定义其背景色为黑色,输出文字为“This is layout 1!”
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/layout1"
/>
<Button
android:id="@+id/Button01"
android:text="Go to layout 2!"
android:layout_marginLeft="0px"
android:layout_marginTop="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
3、ex03_9/res/layout/mylayout.xml
在mylayout.xml中定义其背景色为白色,输出文字为"This is layout 2!"
<?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"
android:background="@drawable/white"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/layout2"
/>
<Button
android:id="@+id/Button02"
android:text="Go to layout 1!"
android:layout_marginLeft="0px"
android:layout_marginTop="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
4、ex03_9/res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="gray">#808080FF </drawable>
<drawable name="white">#FFFFFFFF</drawable>
</resources>
5、ex03_9/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">This is Layout 1!</string>
<string name="app_name">ex03_09</string>
<string name="layout1">This is Layout 1!</string>
<string name="layout2">This is Layout 2!</string>
</resources>
运行结果:
![](http://hi.csdn.net/attachment/201012/15/0_1292391815Cy7y.gif)
程序:
1、ex03_9/src/com.example.ex03_09/ex03-09.java
在主程序中预加载的Layout是main.xml,屏幕上显示的是黑色背景的“This is Layout 1 !”,在第一个layout上的按钮被点击的同时,改变Activity的layout为mylayout.xml,屏幕上显示变成白色背景的“This is layout 2 !”,并利用Button点击时,调用方法的不同做两个layout之间的切换。
package com.ezample.ex03_09;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ex03_09 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*以findViewById()取得Button对象,并添加onClickListener*/
Button b1=(Button)findViewById(R.id.Button01);
b1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
jumpToLayout2();
}
});
}
/*method jumpToLayout2:将layout由main.xml切换成mylayout.xml*/
public void jumpToLayout2()
{
/*将layout改成mylayout.xml*/
setContentView(R.layout.mylayout);
/*以findViewById()取得Button对象,并添加onClickListener*/
Button b2=(Button)findViewById(R.id.Button02);
b2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
jumpToLayout1();
}
});
}
/*method jumpToLayout1:将layout由mylayout.xml切换成main.xml*/
public void jumpToLayout1()
{
/*将layout改成mylayout.xml*/
setContentView(R.layout.main);
/*以findViewById()取得Button对象,并添加onClickListener*/
Button b1=(Button)findViewById(R.id.Button01);
b1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
jumpToLayout2();
}
});
}
}
2、ex03_9/res/layout/main.xml
为了突出layout间切换的效果,特别改变了两个个layout的背景色以及输出文字。在main.xml中定义其背景色为黑色,输出文字为“This is layout 1!”
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/layout1"
/>
<Button
android:id="@+id/Button01"
android:text="Go to layout 2!"
android:layout_marginLeft="0px"
android:layout_marginTop="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
3、ex03_9/res/layout/mylayout.xml
在mylayout.xml中定义其背景色为白色,输出文字为"This is layout 2!"
<?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"
android:background="@drawable/white"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/layout2"
/>
<Button
android:id="@+id/Button02"
android:text="Go to layout 1!"
android:layout_marginLeft="0px"
android:layout_marginTop="10px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
4、ex03_9/res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="gray">#808080FF </drawable>
<drawable name="white">#FFFFFFFF</drawable>
</resources>
5、ex03_9/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">This is Layout 1!</string>
<string name="app_name">ex03_09</string>
<string name="layout1">This is Layout 1!</string>
<string name="layout2">This is Layout 2!</string>
</resources>
相关文章推荐
- Android中级教程(一)之----手机页面的转换setContentView的应用
- Android学习笔记(八) —— 手机页面的转换 — setContentView的应用
- Android中级教程(一)之----手机页面的转换setContentView的应用
- Android初级教程十——手机页面的转换setContentView的应用
- Android中级教程(一)之----手机页面的转换setContentView的应用 转
- Android中级教程(一)之----手机页面的转换setContentView的应用
- Android中级教程(一)之----手机页面的转换setContentView的应用
- 手机页面转换--关于setContentView的应用
- 手机页面的切换(setContentView的应用)
- 手机页面的转换 通过setContentView设置页面布局main.xml 跳转
- 【页面】手机页面跳转的实现方法(一)——setContentView
- Android中级教程之(四)----->页面的转换,利用setContentView方法
- Android 页面间的转换(setContentView)
- setContentView前面写代码,进入页面前的操作
- Android应用setContentView与LayoutInflater加载解析机制源码分析
- Android应用setContentView与LayoutInflater加载解析机制源码分析(超级棒!)
- Android应用setContentView与LayoutInflater加载解析机制源码分析
- Android应用setContentView与LayoutInflater加载解析机制源码分析
- setContentView+LayoutInflater=完美切换页面(两者一定要同时使用,setContentView提高切换页面速度必看)setContentView的秘密----续上
- Android应用setContentView与LayoutInflater加载解析机制源码分析