setContentView()实现布局页面的跳转
来源:互联网 发布:ubuntu 16 输入法 编辑:程序博客网 时间:2024/06/12 21:27
在学习代码的过程中发现android开发中如果想实现布局页面的跳转可以使用setContentView()设置跳转到需要的布局文件上面,实现代码如下:
Java代码
主布局的mai.xml
Java代码
Java代码
package com.google.and; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; /** * * @author google * 实现不同Layout的转换功能,setContentview()用法; */ public class setContentViewDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 以findViewById()取得Button对象并添加事件onClickLisener Button button1=(Button)findViewById(R.id.bt1); button1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { goToLayout2(); }}); } // 将layout由main.xml切换成mylayout.xml public void goToLayout2() { // 将layout改成mylayout setContentView(R.layout.mylayout); Button b2 = (Button) findViewById(R.id.bt2); b2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { goToLayout1(); } }); } // 将layout由mylayout.xml切换成main.xml public void goToLayout1() { setContentView(R.layout.main); Button bt1 = (Button) findViewById(R.id.bt1); bt1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { goToLayout2(); } }); } }
主布局的mai.xml
Java代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://google.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="欢迎来到goole_acmer的博客" /> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击进入Layout2" /> </LinearLayout> 需要跳转到的布局文件mylayout.xmlJava代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://google.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Welcom to my bog" /> <Button android:id="@+id/bt2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击进入Laout1" /> </LinearLayout>
0 0
- setContentView()实现布局页面的跳转
- 【页面】手机页面跳转的实现方法(一)——setContentView
- 使用setContentView实现跳转
- Android 使用setContentView来实现Acitivity的跳转
- 跳转页面的实现
- 页面自动跳转的实现
- JS实现页面的跳转
- PHP实现页面的跳转
- js实现页面的跳转
- android页面跳转的实现
- 实现页面的自动跳转
- js页面跳转的实现
- Intent实现页面的跳转
- Navigator实现页面的跳转
- Flex 页面跳转 页面布局
- JQuery实现页面跳转 页面跳转的七种方法
- 切换Activity中布局的setContentView( )方法
- 从setContentView谈谈android的布局层级
- Android 4.4 SD卡文件读写变化
- 关于AOE网求关键路径
- 堆排序算法
- Android 应用程序窗体显示状态操作(requestWindowFeature()的应用)
- Struts2数据传输的背后机制:ValueStack(值栈)
- setContentView()实现布局页面的跳转
- getResources()从应用中获取资源
- uva 1394 - And Then There Was One(约瑟夫环)
- android小知识(持续更新)
- Emacs 文本编辑器
- ListView讲解
- 暂停匆忙的脚步
- GL音乐播放器<一>---项目计划书
- RelativeLayout相对布局