setContentView(转)

android开发中如果想实现布局页面的跳转可以使用setContentView()设置跳转到需要的布局文件上面,实现代码如下:

Java代码  收藏代码
  1. package com.lyj.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. /** 
  8.  *  
  9.  * @author lyj 
  10.  *  实现不同Layout的转换功能,setContentview()用法; 
  11.  */  
  12. public class setContentViewDemo extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         // 以findViewById()取得Button对象并添加事件onClickLisener  
  20.         Button button1=(Button)findViewById(R.id.bt1);  
  21.         button1.setOnClickListener(new Button.OnClickListener(){  
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 goToLayout2();  
  25.                   
  26.                   
  27.         }});  
  28.          
  29.     }  
  30.     // 将layout由main.xml切换成mylayout.xml  
  31.     public void goToLayout2() {  
  32.            // 将layout改成mylayout  
  33.            setContentView(R.layout.mylayout);  
  34.            Button b2 = (Button) findViewById(R.id.bt2);  
  35.            b2.setOnClickListener(new Button.OnClickListener() {  
  36.                public void onClick(View v) {  
  37.                    goToLayout1();  
  38.                }  
  39.            });  
  40.        }   
  41.     // 将layout由mylayout.xml切换成main.xml  
  42.     public void goToLayout1() {  
  43.         setContentView(R.layout.main);  
  44.         Button bt1 = (Button) findViewById(R.id.bt1);  
  45.         bt1.setOnClickListener(new Button.OnClickListener() {  
  46.             public void onClick(View v) {  
  47.                 goToLayout2();  
  48.             }  
  49.         });  
  50.     }   
  51.   
  52. }  

 主布局的mai.xml

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="欢迎来到小李的博客"  
  11.     />  
  12.  <Button  
  13.     android:id="@+id/bt1"  
  14.     android:layout_width="wrap_content"  
  15.     android:layout_height="wrap_content"  
  16.     android:text="点击进入Layout2"  
  17.  />  
  18. </LinearLayout>  

需要跳转到的布局文件mylayout.xml

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#ffffffff"   
  7.     >  
  8. <TextView    
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:text="Welcom to my bog"  
  12.     />  
  13.  <Button  
  14.     android:id="@+id/bt2"  
  15.     android:layout_width="fill_parent"  
  16.     android:layout_height="wrap_content"  
  17.     android:text="点击进入Laout1"  
  18.     />  
  19. </LinearLayout>  
原文地址:https://www.cnblogs.com/altman29/p/4969518.html