之前有看视频了解手机页面的切换用2个Activity进行切换,今天看了下书,采用的Flayout进行切换,特做笔记。
小心得:主要利用setContentView(R.layout.activity_main2)切换Layout。代码如下。
1 package com.example.test02; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 import android.view.View; 7 import android.widget.Button; 8 9 public class MainActivity extends Activity {10 Button b1 =null;11 Button b2 =null;12 @Override13 public void onCreate(Bundle savedInstanceState) {14 super.onCreate(savedInstanceState);15 setContentView(R.layout.activity_main);16 b1 = (Button) findViewById(R.id.button1);17 b1.setOnClickListener( new Button.OnClickListener(){18 19 public void onClick(View v) {20 switchLayoutOne();21 22 }23 24 } );25 }26 public void switchLayoutOne (){27 setContentView(R.layout.activity_main2);28 b2 = (Button) findViewById(R.id.button2);29 b2.setOnClickListener( new Button.OnClickListener(){30 public void onClick(View v){31 switchLayoutTwo();32 }33 });34 }35 public void switchLayoutTwo (){36 setContentView(R.layout.activity_main);37 b1 = (Button) findViewById(R.id.button1);38 b1.setOnClickListener( new Button.OnClickListener(){39 public void onClick(View v){40 switchLayoutOne();41 }42 });43 }44 @Override45 public boolean onCreateOptionsMenu(Menu menu) {46 getMenuInflater().inflate(R.menu.activity_main, menu);47 return true;48 }49 }