Android ApiDemos example resolution (193): Views-& gt; Tabs-& gt; Content

Source: Internet
Author: User

This example introduces TabActivity. In the new Android version, TabActivity is not recommended, but Fragment. TabActivity provides a Tab Control. The key class is TabHost and TabHost is the View of TabActivity. You can use getTabHost () to obtain the TabHost corresponding to TabActivity.

You can use TabHost. tabSpec is used to add a page to the Tab window. TabSpec is used to describe a page (Tab). Each Tab can have a tag to distinguish a page in the Tab window, you can use text, icons, etc. to display) and Content (page Content ).

Content is constructed using the View resource Id, TabHost. TabContentFactory, or an Intent instance (the Intent can start an Activity ). This is why TabActivity is an ActivityGroup.

In this example, the corresponding code Tab1.java uses the View resource ID method to set the Content of the Tab:


[Java]
TabHost tabHost = getTabHost ();
 
LayoutInflater. from (this). inflate (R. layout. tabs1,
TabHost. getTabContentView (), true );
 
TabHost. addTab (tabHost. newTabSpec ("tab1 ")
. SetIndicator ("tab1 ")
. SetContent (R. id. view1 ));
TabHost. addTab (tabHost. newTabSpec ("tab3 ")
. SetIndicator ("tab2 ")
. SetContent (R. id. view2 ));
TabHost. addTab (tabHost. newTabSpec ("tab3 ")
. SetIndicator ("tab3 ")
. SetContent (R. id. view3 ));

TabHost tabHost = getTabHost ();

LayoutInflater. from (this). inflate (R. layout. tabs1,
TabHost. getTabContentView (), true );

TabHost. addTab (tabHost. newTabSpec ("tab1 ")
. SetIndicator ("tab1 ")
. SetContent (R. id. view1 ));
TabHost. addTab (tabHost. newTabSpec ("tab3 ")
. SetIndicator ("tab2 ")
. SetContent (R. id. view2 ));
TabHost. addTab (tabHost. newTabSpec ("tab3 ")
. SetIndicator ("tab3 ")
. SetContent (R. id. view3 ));

 

Note that you must use the View specified in setContent to expand them to tabHost. getTabContentView.

R. layout. tabs1.xml is defined as follows:

[Html]
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
 
<TextView android: id = "@ + id/view1 ″
Android: background = "@ drawable/blue"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_sharetab_1"/>
 
<TextView android: id = "@ + id/view2 ″
Android: background = "@ drawable/red"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_1_tab_2"/>
 
<TextView android: id = "@ + id/view3 ″
Android: background = "@ drawable/green"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_1_tab_3"/>
 
</FrameLayout>

<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">

<TextView android: id = "@ + id/view1 ″
Android: background = "@ drawable/blue"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_sharetab_1"/>

<TextView android: id = "@ + id/view2 ″
Android: background = "@ drawable/red"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_1_tab_2"/>

<TextView android: id = "@ + id/view3 ″
Android: background = "@ drawable/green"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = "@ string/tabs_1_tab_3"/>

</FrameLayout>

 

 


 

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.