您的位置:首页 > 其它

手机页面的转换----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。

运行结果:





程序:

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐