One Android tabhost layout inherits tabactivity and uses activity Layout

Source: Internet
Author: User

Check the final information first:

 

 

Let's look at the code structure:

 

 

Among them, black.gif is a black background image, and grey.gif is a gray background image.

 

Then directly go to the Code:

Artistactivity. Java

 

Package cn.com. tagview; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; </P> <p> public class artistactivity extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> textview = new textview (this); <br/> // This document is displayed as the label content <br/> textview. settext ("artistic content"); <br/> setcontentview (textview); </P> <p >}< br/>

Musicactivity. Java

Package cn.com. tagview; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; </P> <p> public class musicactivity extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> textview = new textview (this); <br/> // This document is displayed as the label content <br/> textview. settext ("music content"); <br/> setcontentview (textview); <br/>}< br/>

Sportactivity. Java

Package cn.com. tagview; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; </P> <p> public class sportactivity extends activity {</P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> textview = new textview (this); <br/> // This document is displayed as the label content <br/> textview. settext ("Sports content"); <br/> setcontentview (textview); </P> <p >}< br/>

 

Artistactivity. Java musicactivity. Java sportactivity. Java is a tag-based activity. That is, when you click the corresponding tag, the corresponding activity content is displayed below.

 

 

Ic_tab.xml code

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <selector <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> <item Android: drawable = "@ drawable/gray" <br/> Android: state_selected = "true" <br/> </item> <br/> <item Android: drawable = "@ drawable/Black" </P> <p> </item> <br/> </selector> <br/>

Note that the location of the ic_tab.xml file is stored in the Res/drawable folder. Some friends say that this folder does not exist. In fact, you can see that I put it in drawable-hdpi, in fact, drawable-hdpi, drawable-ldpi, and drawable-mdpi all belong to the drawable folder. This file specifies the image displayed on the tag when the tag gets the focus and loses the focus.

For example, in this example, when state_selected = "true" (when the tag is selected), the resource image specified by @ drawable/gray is displayed. When not selected, the resource image specified by @ drawable/Black is displayed.

Tagview. Java code:

Package cn.com. tagview; </P> <p> Import android. app. tabactivity; <br/> Import android. content. intent; <br/> Import android. content. res. resources; <br/> Import android. OS. bundle; <br/> Import android. widget. tabhost; </P> <p>/** <br/> * @ author chenzheng_java <br/> * @ description note, this class must inherit tabactivity <br/> */<br/> public class tagview extends tabactivity {<br/> @ override <br/> Public void oncreate (bundle save Dinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> // setcontentview (R. layout. main); <br/> // a class for accessing application resources in Android Code <br/> resources Resources = getresources (); <br/> // obtain the tag of the current activity. setcontentview (COM. android. internal. r. layout. tab_content); <br/> tabhost = gettabhost (); <br/> // each tag item <br/> tabhost. tabspec spec; </P> <p> // declare an intent, which tells us that the next activity to jump to is Artista. Ctivity. <Br/> intent = new intent (this, artistactivity. class); <br/>/** <br/> * tabhost. newtabspec ("artist") creates a tag item, where artist is its tag identifier, which is equivalent to the name attribute of the JSP page tag <br/> * setindicator ("Art tag", resources. getdrawable (R. drawable. ic_tab) set the label to display the text and the icon on the label (this icon is not an image, but an XML file) <br/> * setcontent (intent) specify an intent for the current tag <br/> * tabhost. addtab (SPEC); Add the tag item to the tag <br/> */<br/> spec = tabhost. newtabspec ("artist "). setindicator ("Art tag", <br/> resources. getdrawable (R. drawable. ic_tab )). setcontent (intent); <br/> tabhost. addtab (SPEC); </P> <p> intent intent2 = new intent (this, musicactivity. class); <br/> spec = tabhost. newtabspec ("Music "). setindicator ("Music tag", <br/> resources. getdrawable (R. drawable. ic_tab )). setcontent (intent2); <br/> tabhost. addtab (SPEC); </P> <p> intent intent3 = new intent (this, sportactivity. class); <br/> spec = tabhos T. newtabspec ("sport "). setindicator ("Sports tag", <br/> resources. getdrawable (R. drawable. ic_tab )). setcontent (intent3); <br/> tabhost. addtab (SPEC); </P> <p> // tabhost. setcurrenttabbytag ("Music"); sets the default label displayed when the first time the tag is opened. newtabspec ("Music") has the same parameters <br/> tabhost. setcurrenttab (1); // set the default label displayed when the tag is opened for the first time. The parameter indicates the order in which the tag is added. The position starts from 0. </P> <p >}< br/>}

 

Androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "cn.com. tagview "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "8"/> </P> <p> <application Android: icon = "@ drawable/icon" Android: label = "@ string/app_name"> <br/> <! -- Android: theme = "@ Android: style/theme. notitlebar "means to remove the default tag from the system and leave the default location for our own tag --> <br/> <activity Android: Name = ". tagview "<br/> Android: Label =" @ string/app_name "<br/> Android: theme =" @ Android: style/theme. notitlebar "<br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </Activity> <br/> <! -- Declare the three activities used for tag switching in the main configuration file. Remember to declare them here; otherwise, an error occurs. <br/> Android: name = "artistactivity" indicates whether artistactivity exists before artistactivity. you only need to ensure that the class is in the package attribute package under the manifest label. <Br/> --> <br/> <activity Android: Name = "artistactivity" Android: label = "@ string/app_name"> </activity> <br/> <activity Android: Name = "musicactivity" Android: label = "@ string/app_name"> </activity> <br/> <activity Android: Name = "sportactivity" Android: label = "@ string/app_name"> </activity> </P> <p> </Application> <br/> </manifest>

 

After everything is done, the final result is displayed. Note that main. XML has never been used.

Waste of words:

In fact, the tabhost layout has many similarities with listview, and the system also provides them with a help class, tabhost-tabactivity listview-listactivity. after our activity integrates these classes, we generally only need to sort and bind the data.

Again, the Code contains a call to the setcontentview method, but because tabactivity is integrated, the gettabhost method of tabactivity has been implemented. Hiding a user does not mean no.

To make the project simple and easy to understand, we only add a text in the content section of each tag. In fact, we can add images, videos, and so on. You only need to implement it in the corresponding activity. We can see that this method actually has a good layered structure, and there is not much coupling between the activity and the activity.

It may have been until now that some friends have made a special twist on tabactivity and listactivity. Here I will give you a brief description. In fact, this is actually a design mode, template mode. The system provides you with a template that implements most of the content, and then you inherit the template to make modifications (for example, there is a method in the template that has no implementation, you can rewrite this method and implement it) to make it meet your requirements. This is the principle of the template mode.

 

 

 

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.