Hello everyone, this section describes the conversion of mobile phone pages.Setcontentview
In the world of web pages, to switch between two pages, you only need to use hyperlinks,
But in the world of mobile phones, how can we convert mobile pages? The simplest way is to changeActivity
OfLayout
!
In this example, twoLayout
, RespectivelyLayout1 (main. XML)
AndLayout2 (mylayout. XML ),
DefaultLayout
IsMain. XML,
InLayout1
Create a button. When you click the button, the second button is displayed.Layout (mylayout. XML)
; Similarly, inLayout2
Also design a button, When you click the secondLayout
Is displayed back to the originalLayout1
To demonstrate how to switch between two pages.
First, let's take a look (to distinguish twoLayout
, We set different background colors respectively ):
The following is the Code involved in this program. The first is the layout of the main interface.Main. xml
<? 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 = "Welcome to Wei zhulin's blog"
/>
<Button
Android: Id = "@ + ID/Bt1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "click to enter layout2"
/>
</Linearlayout>
Secondly, weMain. xml
Create a newMylayout. xml
File, the Code is as follows:
<? 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 = "# ffffffff"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "welcome to Mr Wei's blog"
/>
<Button
Android: Id = "@ + ID/bt2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "click to enter layout1"
/>
</Linearlayout>
Finally, our core programSetcontentviewdemo. Java
Package com. Android. setcontentviewdemo;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. Button;
Public class setcontentviewdemo extends activity {
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Load main. xml Layout
Setcontentview (R. layout. Main );
// Obtain the button object with findviewbyid () and add the event onclicklisener
Button Bt1 = (button) findviewbyid (R. Id. Bt1 );
Bt1.setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
Gotolayout2 ();
}
});
}
// Switch layout from Main. XML to mylayout. xml
Public void gotolayout2 (){
// Change layout to mylayout.
Setcontentview (R. layout. mylayout );
Button b2 = (button) findviewbyid (R. Id. bt2 );
B2.setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
Gotolayout1 ();
}
});
}
// Switch layout from mylayout. XML to 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 ();
}
});
}
}
Last executed !, This section ends now ~