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

原创粉丝点击