The layout method for using Tabhost components to inherit tabactivity in Android applications _android

Source: Internet
Author: User

Inherit tabactivity and lay out the activity
first look at the final effect chart:

Look at the code structure again:

Which black.gif as the name suggests is a black background picture, Grey.gif is a gray background picture

Then go directly to the code:
Artistactivity.java

Package Cn.com.tagview; 
 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.widget.TextView; 
 
public class Artistactivity extends activity { 
 
  @Override 
  protected void onCreate (Bundle savedinstancestate) { C8/>super.oncreate (savedinstancestate); 
    TextView TextView = new TextView (this); 
    The document will be displayed as the contents of the label 
    textview.settext ("Art content"); 
    Setcontentview (TextView); 
     
  } 
 

Musicactivity.java

Package Cn.com.tagview; 
 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.widget.TextView; 
 
public class Musicactivity extends activity { 
 
  @Override 
  protected void onCreate (Bundle savedinstancestate) { 
    super.oncreate (savedinstancestate); 
    TextView TextView = new TextView (this); 
    The document will be displayed as the contents of the label 
    Textview.settext ("Music content"); 
    Setcontentview (TextView); 
  } 
 

Sportactivity.java

Package Cn.com.tagview; 
 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.widget.TextView; 
 
public class Sportactivity extends activity { 
 
  @Override 
  protected void onCreate (Bundle savedinstancestate) { C22/>super.oncreate (savedinstancestate); 
    TextView TextView = new TextView (this); 
    The document will be displayed as the contents of the label 
    Textview.settext ("Motion content"); 
    Setcontentview (TextView); 
     
  } 
 

Artistactivity.java Musicactivity.java Sportactivity.java Three activity is an activity that is used as a label content. That is, when the user clicks the corresponding label, the following will display the corresponding activity content.

Ic_tab.xml Code

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android= 
 "http://schemas.android.com/apk/" Res/android " 
 > 
 <item android:drawable=" @drawable/grey " 
    android:state_selected=" true " 
 > </item> 
 <item android:drawable= "@drawable/black" 
     
 ></item> 
</selector> 

Be sure to note that the location of the Ic_tab.xml file is placed under the Res/drawable folder. Some friends say how does not have this folder Ah, actually everybody saw I put it in the drawable-hdpi, actually drawable-hdpi, drawable-ldpi, drawable-mdpi three folders belong to the Drawable folder. It sets out what pictures are displayed on the label when the label gets focus and loses focus.

For example, in this example, when state_selected= "true" (when the label is selected), the resource picture specified by @drawable/grey is displayed. Displays the resource picture specified by @drawable/black when it is not selected.

Tagview.java Code:

Package Cn.com.tagview; 
Import android.app.TabActivity; 
Import android.content.Intent; 
Import android.content.res.Resources; 
Import Android.os.Bundle; 
 
Import Android.widget.TabHost; 
  /** * @author Chenzheng_java * @description Note that this class must inherit the Tabactivity/public class Tagview extends Tabactivity { 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.main); 
    A class of resources in the Android code to access the application resource = Getresources (); 
    Gets the label of the current activity, which has been implemented in the implementation of Setcontentview (com.android.internal.r.layout.tab_content); 
    Tabhost tabhost = Gettabhost (); 
 
    Each label item TABHOST.TABSPEC spec; 
    Declare an intention that tells us that the next activity to jump to is artistactivity. 
    Intent Intent = new Intent (this, artistactivity.class); /** * TABHOST.NEWTABSPEC ("artist") creates a label item in which artist is its label identifier, which is equivalent to the name attribute of the JSP page label * SETINDICATOR ("Art label", RESOURCES.G Etdrawable (R.drawable.ic_tab)) Set label display text and labelsThe icon on (the icon is not a picture, but an XML file OH) * SetContent (Intent) assigns an intent to the current label * TABHOST.ADDTAB (SPEC); Add a label item to the label/spec = Tabhost.newtabspec ("artist"). Setindicator ("Art label", Resources.getdrawable (r.drawabl 
    E.ic_tab)). SetContent (Intent); 
 
    Tabhost.addtab (spec); 
    Intent Intent2 = new Intent (this, musicactivity.class); Spec = Tabhost.newtabspec ("Music"). Setindicator ("Musical label", Resources.getdrawable (R.drawable.ic_tab)). SetContent (inte 
    NT2); 
 
    Tabhost.addtab (spec); 
    Intent Intent3 = new Intent (this, sportactivity.class); Spec = Tabhost.newtabspec ("Sport"). Setindicator ("Sport label", Resources.getdrawable (R.drawable.ic_tab)). SetContent (inte 
    NT3); 
 
    Tabhost.addtab (spec); 
    Tabhost.setcurrenttabbytag ("Music"), which is the same as the Tabhost.newtabspec ("music") of the label that is displayed by default on the first open 
 
  Tabhost.setcurrenttab (1)//Set the label that is displayed by default the first time it is opened, the parameter represents the order in which it was added to the label, and the position is starting at 0. 

 } 
}

Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Cn.com.tagview "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk androi d:minsdkversion= "8"/> <application android:icon= "@drawable/icon" android:label= "@string/app_name" > ;! --Android:theme= "@android: Style/theme.notitlebar" means to remove the system's default tag tag, to empty out the position for our own label--> <activity android:name= ". Tagview "android:label=" @string/app_name "Android:theme=" @android: Style/theme.notitlebar "&G" 
      T <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:na Me= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!--declared in the main configuration file for the label Check the 3 activity of the switch, remember to declare it here, otherwise it will go wrong android:name= "artistactivity" 
     Inside artistactivity whether there is. All right, you just have to make sure that the class is in the package under the Manifest tab package attributes. 
   --> <activity android:name= "artistactivity" android:label= "@string/app_name" ></activity> <activity Andr Oid:name= "musicactivity" android:label= "@string/app_name" ></activity> <activity android:name= " 

 Sportactivity "android:label=" @string/app_name "></activity> </application> </manifest>

When everything is done, it runs, and it comes to a final effect. Here to note that Main.xml is never used to the OH.

Gums

In fact, the use of tabhost layout and ListView have a lot of similarities, the system also provides them with a Help class, tabhost-tabactivity listview-listactivity. When our activity integrates these classes, In general, we just need to organize and bind the data.
Again, there is a call to the Setcontentview method in the code, only because we have implemented it in the Gettabhost method that we have integrated into the tabactivity,tabactivity. Hidden to the user, does not mean not.
In order to be easy to understand in the project, we just added a text to the content section of each label. In fact, we can add pictures, videos and so on inside. Just do it in the appropriate activity. As we can see, this approach has a very good layered structure, and there is not much coupling between activity and activity.
Perhaps until now, some friends of tabactivity and listactivity This implementation is particularly awkward. I'm simply saying here, actually this is a design pattern, template pattern. The system provides you with a template that implements most of the content, and then you modify it by inheriting the template (for example, there's a method in the template that doesn't have any implementation, you rewrite the method and implement it specifically) to meet your requirements. This is how the template pattern works.

Inherit tabactivity and lay out the layout file
then look at the layout of the XML layout file method, first on the effect chart:

Above is the final effect chart.
The code structure is as follows.

Main.xml Code:

<?xml version= "1.0" encoding= "Utf-8"?> <!--the layout file defines the content section of the label, which must be framelayout as the root element--> Xmlns:android= "Http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" Android:layout_ height= "Fill_parent" > <!--the first label content--> <linearlayout android:id= "@+id/widget_layout_blue" android:l Ayout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <EditText A Ndroid:id= "@+id/widget34" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= " EditText "android:textsize=" 18SP "> </EditText> <button android:id=" @+id/widget30 "Android:lay Out_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "button" > </Button> 
    ;/linearlayout> <!--the second label content AnalogClock for timepiece components--> <linearlayout android:id= "@+id/widget_layout_red" Android:layout_width= "Fill_parent" Android:layout_height= "fill_parent" android:orientation= "vertical" > <analogclock android:id= "@+id/widget36" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" > </AnalogClock> </linea  
    Rlayout> <!--Third label content RadioButton must be in Radiogroup oh--> <linearlayout android:id= "@+id/widget_layout_green" 
    Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <radiogroup android:id= "@+id/widget43" android:layout_width= "166px" android:layout_height= "98px" Android: orientation= "vertical" > <radiobutton android:id= "@+id/widget44" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "RadioButton" > </RadioButton> <radiob 
        Utton android:id= "@+id/widget45" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:text= "RadiobutTon "> </RadioButton> </RadioGroup> </LinearLayout> </FrameLayout> 

 

Taghosttest.java Code:

Package cn.com.tagHost.test; 
Import android.app.TabActivity; 
Import Android.graphics.Color; 
Import Android.os.Bundle; 
Import Android.view.LayoutInflater; 
Import Android.view.ViewGroup; 
 
Import Android.widget.TabHost; 
 
  public class Taghosttest extends Tabactivity {private Tabhost mytabhost; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
    Mytabhost = This.gettabhost (); /** * Inflate (int resource, ViewGroup root, Boolean attachtoroot) * Resource is obviously a resource index ID * when Attachtoroo When T is true, root represents a component that can be placed in a container * when Attachtoroot is false, root represents only one object that stores the value * The method means that the label view that is generated by R.layout.main will be added to the Myta  The inflate method of the * Layoutinflater class in the parent container obtained by Bhost.gettabcontentview () has the following fragment * if (root!= null && attachtoroot) 
          {Root.addview (temp, params); 
      Where temp is a label-related view/Layoutinflater.from (this) that is generated according to the resource specified by resource. Inflate (R.layout.main,  Mytabhost.gettabcontentview (), true); 
 
    Mytabhost.setbackgroundcolor (Color.argb (150, 22, 70, 150)); 
 
    Mytabhost.addtab (Mytabhost.newtabspec ("one"). Setindicator ("A"). SetContent (R.id.widget_layout_blue)); 
        Mytabhost.addtab (Mytabhost.newtabspec ("two"). Setindicator ("B", Getresources (). getdrawable (R.drawable.icon)) 
 
    . SetContent (R.id.widget_layout_green)); 
        Mytabhost.addtab (Mytabhost.newtabspec ("Three"). Setindicator ("C", Getresources (). getdrawable (R.drawable.icon)) 
  . SetContent (r.id.widget_layout_red)); 

 } 
}

This approach is easier to achieve and see what we've done.
Step one: Define the layout file for the Label Content section, which must be framelayout as the root node.
Step two: Let the activity inherit tabactivity, and then implement your own code.


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.