Implementation Method 1 of Tab pages for Android -- Combination of TabActivity and TabHost (1)

Source: Internet
Author: User

 

Note: The following four articles are all transferred fromHttp://blog.csdn.net/kkfdsa132

 

Many software, because of its many functions, prefer to use Tab pages. In Android, there are two common tabs:

1. Use TabActivity and TabHost combination

2. ActivityGroup and GridView are used.

Here we will talk about their implementation methods one by one. Now, let's talk about the combination of TabActivity and TabHost. In fact, there are three ways to combine TabActivity and TabHost:

Method 1: The layout of each page is placed in the same file, and the code is also compact. Not recommended.

Method 1: The layout files of each page are separated, but the code is still compact and is not recommended.

Method 3: The layout files of each page are separated, and the code of each page is separated. We recommend that you.

There is no difference, how to reflect it; here we will describe three methods one by one, and the readers will gradually understand it.

(1) Implementation of TabActivity and TabHost combination of pagination labels ------- method 1

Implementation Details:

1. The main class inherits TabActivity

Public class Pagination extends TabActivity

2. Get the current TabHost object

TabHost tabHost = getTabHost ();

3. Load the layout File

LayoutInflater. from (this). inflate (R. layout. main, tabHost. getTabContentView (), true );

4. Add Tab pages

TabHost. addTab (tabHost. newTabSpec ("tab1 ")
. SetIndicator ("tab1", getResources (). getDrawable (R. drawable. a1 ))
. SetContent (R. id. view1 ));

.........

1. layout file: main. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent"> <br/> <! -- Pagination 1 --> <br/> <TextView android: id = "@ + id/view1" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent" <br/> android: text = "This is Tab1"/> </p> <! -- Page 2 --> <br/> <LinearLayout <br/> android: id = "@ + id/view2" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent" <br/> android: orientation = "vertical" <br/> <EditText <br/> android: id = "@ + id/et_text" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> android: text = "EditText" <br/> android: textSize = "18sp" <br/> </EditText> <br/> <Bu Tton <br/> android: id = "@ + id/bt_show" <br/> android: layout_width = "149px" <br/> android: layout_height = "wrap_content" <br/> android: text = "show" <br/> </Button> <br/> </LinearLayout> </p> <! -- Pagination 3 --> <br/> <TextView android: id = "@ + id/view3" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent" <br/> android: text = "This is Tab3"/> </p> </FrameLayout> <br/>

 

2. Code

Package com. myandroid. test; </p> <p> import android. app. tabActivity; <br/> import android. OS. bundle; <br/> import android. view. layoutInflater; <br/> import android. view. view; <br/> import android. view. view. onClickListener; <br/> import android. widget. button; <br/> import android. widget. editText; <br/> import android. widget. tabHost; <br/> import android. widget. toast; </p> <p> public class Pagination extends TabActivity {<br/> private Button bt_show; <br/> private EditText et_text; <br/>/** Called when the activity is first created. */<br/> @ Override <br/> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); <br/> // setContentView (R. layout. main); // you do not need to load the home page <br/> TabHost tabHost = getTabHost (); <br/> LayoutInflater. from (this ). inflate (R. layout. main, // load the page <br/> tabHost. getTabContentView (), true); <br/> tabHost. addTab (tabHost. newTabSpec ("tab1") <br/>. setIndicator ("tab1", getResources (). getDrawable (R. drawable. a1) <br/>. setContent (R. id. view1); // set the control to be displayed on the page <br/> tabHost. addTab (tabHost. newTabSpec ("tab3") <br/>. setIndicator ("tab2", getResources (). getDrawable (R. drawable. a2) <br/>. setContent (R. id. view2); <br/> tabHost. addTab (tabHost. newTabSpec ("tab3") <br/>. setIndicator ("tab3", getResources (). getDrawable (R. drawable. a3) <br/>. setContent (R. id. view3); </p> <p> bt_show = (Button) findViewById (R. id. bt_show); <br/> et_text = (EditText) findViewById (R. id. et_text); <br/> bt_show.setOnClickListener (new ClickEvent ()); </p> <p >}</p> <p> class ClickEvent implements OnClickListener {</p> <p> @ Override <br/> public void onClick (View v) {<br/> // TODO Auto-generated method stub <br/> Toast. makeText (Pagination. this, et_text.getText (), Toast. LENGTH_SHORT ). show (); <br/>}</p> <p>}

 

 

Add three images in the drawablefile named a1.png, a2.png, and a3.png.

Next is the paging tag implemented by combining TabActivity and TabHost ------- method 2

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.