Android tab implementation

Source: Internet
Author: User

The Tab is a very convenient component. Today, I know a simple implementation of the android Tab. this article comes from the IT technology learning network (http://www.hopean.com) Source: http://www.hopean.com/devlop/ below is a simple step. 1. use FrameLayout in the layout file to list the Tab component and the content component in the Tab. 2. activity must inherit TabActivity 3. call the getTabHost () method of TabActivity to obtain the TabHost object. 4. after creating a project using the TabHost Tab option, first modify strings. xml file [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> AndroidTabSelector </string> <string name = "hello_world"> Hello world! </String> <string name = "menu_settings"> Settings </string> <string name = "test1"> test interface 1 </string> <string name = "test2"> test interface 2 </string> <string name = "test3"> test interface 3 </string> </resources> and then modify main. xml file, detailed code: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/FrameLayout01" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <TabHost android: id = "@ + id/TabHost01" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <TextView android: id = "@ + id/TextView01" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "@ string/test1"/> <TextView android: id = "@ + id/TextView02" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "@ string/test2"/> <TextView android: id = "@ + id/TextView03" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "@ string/test3"/> </FrameLayout> and then modify MainActivity. java file. The java code is as follows: [java] package com. example. androidtabselector; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. app. tabActivity; import android. OS. bundle; import android. view. layoutInflater; import android. widget. tabHost; public class MainActivity extends TabActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // setContentView (R. layout. activity_main); TabHost tah = getTabHost (); // from (this) Get LayoutInflater from TabActivity // R. layout. main stores the Tab layout // obtains the FrameLayout that stores the Tab content through TabHost // determines whether to add inflate to the root layout element LayoutInflater. from (this ). inflate (R. layout. activity_main, tah. getTabContentView (), true); // set the content of the Tab and the tah. addTab (tah. newTabSpec ("tab1 "). setIndicator ("option 1 "). setContent (R. id. textView01); tah. addTab (tah. newTabSpec ("tab2 "). setIndicator ("option 2 "). setContent (R. id. textView02); tah. addTab (tah. newTabSpec ("tab3 "). setIndicator ("option 3 "). setContent (R. id. textView03);} @ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. activity_main, menu); return true ;}} now you can implement a simple android tab.

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.