手机页面的转换----setContentView的应用
来源:互联网 发布:宁泽涛 扒皮 知乎 编辑:程序博客网 时间:2024/05/29 13:08
实现手机页面的转换,最简单的方式就是改变Activity的Layout!在这个例子里,将布局两个Layout,分别为Layout1(main.xml)与Layout2(mylayout.xml),默认载入的Layout为main.xml,且在Layout1当中创建一个按钮,当点击按钮时,显示第二个layout(mylayout.xml);同样的,在Layout2里设计一个按钮,当点击第二个Layout的按钮之后,则显示回到原来的Layout1。
运行结果:
程序:
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>
- 手机页面的转换----setContentView的应用
- 手机页面转换--关于setContentView的应用
- Android中级教程(一)之----手机页面的转换setContentView的应用
- Android初级教程十——手机页面的转换setContentView的应用
- Android中级教程(一)之----手机页面的转换setContentView的应用
- Android中级教程(一)之----手机页面的转换setContentView的应用
- Android学习笔记(八) —— 手机页面的转换 — setContentView的应用
- 手机页面的切换(setContentView的应用)
- Android 页面间的转换(setContentView)
- 巧用setContentView的应用
- 【页面】手机页面跳转的实现方法(一)——setContentView
- Android中级教程之(四)----->页面的转换,利用setContentView方法
- setContentView()实现布局页面的跳转
- android学习三 手机页面的转换
- 手机的页面转换及页面间的数据传递
- setContentView前面写代码,进入页面前的操作
- setContentView+LayoutInflater=完美切换页面(两者一定要同时使用,setContentView提高切换页面速度必看)setContentView的秘密----续上
- 3.8 MonoForAndroid用户人机界面-手机页面的转换
- 转载【去跨国公司好还是去创业公司好?】(HW)
- Sokoban(仓库番,推箱子)自动求解与辅助求解工具
- 网页关闭时弹出确认取消对话框
- juicy-couture-handbags-1291689169078
- PHP MYSQL精确测试SQL语句执行时间
- 手机页面的转换----setContentView的应用
- 转贴:生活在Emacs中!
- 在 Windows server 2008 下计划任务无法正常执行bat批处理文件
- Java ME 初步了解
- Java.八位数.小数点后两位.StringBuffer().append()
- Page State
- 棋牌游戏架构
- 我们三十以后才明白
- .NET与 JEE