This time we will introduce the Tab control of Android. The Tab control can achieve the paging effect, so that the content of a screen can be as rich as possible. Of course, it will also increase the complexity of development, use it again when necessary. The Tab control of Android is a bit strange to use. It must contain and follow the following sequence:
TabHost control-> TabWidget (must be named tabs)-> FrameLayout (must be named tabcontent ).
Next, paste the example running:
Source code of main. xml:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<TabHost android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ android: id/TabHost1">
<TabWidget android: id = "@ android: id/tabs"
Android: layout_height = "wrap_content" android: layout_width = "fill_parent">
</TabWidget>
<FrameLayout android: id = "@ android: id/tabcontent"
Android: paddingTop = "65px" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<LinearLayout android: layout_height = "wrap_content" android: id = "@ + id/Tab1" android: orientation = "vertical" android: layout_width = "fill_parent">
<EditText android: layout_height = "wrap_content" android: id = "@ + id/edtTab1" android: layout_width = "fill_parent"> </EditText>
<Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/btnTab1" android: text = "Tab1"> </Button>
</LinearLayout>
<LinearLayout android: layout_height = "wrap_content" android: id = "@ + id/Tab2" android: layout_width = "fill_parent" android: orientation = "horizontal">
<EditText android: layout_height = "wrap_content" android: id = "@ + id/edtTab2" android: layout_width = "wrap_content" android: layout_weight = "300"> </EditText>
<Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/btnTab2" android: text = "Tab2"> </Button> </LinearLayout>
</FrameLayout>
</TabHost>
<? Xml version = "1.0" encoding = "UTF-8"?>
<TabHost android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ android: id/TabHost1">
<TabWidget android: id = "@ android: id/tabs"
Android: layout_height = "wrap_content" android: layout_width = "fill_parent">
</TabWidget>
<FrameLayout android: id = "@ android: id/tabcontent"
Android: paddingTop = "65px" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<LinearLayout android: layout_height = "wrap_content" android: id = "@ + id/Tab1" android: orientation = "vertical" android: layout_width = "fill_parent">
<EditText android: layout_height = "wrap_content" android: id = "@ + id/edtTab1" android: layout_width = "fill_parent"> </EditText>
<Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/btnTab1" android: text = "Tab1"> </Button>
</LinearLayout>
<LinearLayout android: layout_height = "wrap_content" android: id = "@ + id/Tab2" android: layout_width = "fill_parent" android: orientation = "horizontal">
<EditText android: layout_height = "wrap_content" android: id = "@ + id/edtTab2" android: layout_width = "wrap_content" android: layout_weight = "300"> </EditText>
<Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/btnTab2" android: text = "Tab2"> </Button> </LinearLayout>
</FrameLayout>
</TabHost>
Program source code:
View plaincopy to clipboardprint?
Package com. testTab;
Import android. app. TabActivity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TabHost;
Import android. widget. TabHost. TabSpec;
Public class testTab extends TabActivity {// built based on TabActivity
Button btnTab1, btnTab2;
EditText edtTab1, edtTab2;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TabHost tabs = getTabHost ();
// Set Tab1
TabSpec tab1 = tabs. newTabSpec ("tab1 ");
Tab1.setIndicator ("tab1"); // you can specify the tab1 name.
Tab1.setContent (R. id. Tab1); // associate controls
Tabs. addTab (tab1); // Add tab1
BtnTab1 = (Button) this. findViewById (R. id. btnTab1 );
EdtTab1 = (EditText) this. findViewById (R. id. edtTab1 );
BtnTab1.setOnClickListener (new ClickEvent ());
// Set Tab2
TabSpec tab2 = tabs. newTabSpec ("tab2 ");
Tab2.setIndicator ("tab2 ");
Tab2.setContent (R. id. Tab2 );
Tabs. addTab (tab2 );
& Nb