Android Program development custom settings Tabhost,tabwidget style _android

Source: Internet
Author: User

First look at the effect:


Jingdong Mall Bottom Menu bar


Sina Micro Blog Bottom menu bar

This study effect chart:

First, the main layout file (Boot page main.xml, located in the Res/layout directory) code

<?xml version= "." Encoding= "utf-"?> <tabhost xmlns:android= "Http://schemas.android.com/apk/res/android" Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:id= "@+id/tabhost" > <
LinearLayout android:id= "@+id/linear" android:layout_width= "fill_parent" android:layout_height= "Fill_parent"
android:orientation= "vertical" > <tabwidget android:id= "@android: Id/tabs" android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" ></TabWidget> <framelayout android:id= "@android: Id/tabcontent"
Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" > <linearlayout android:id= "@+id/tab" Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > < TextView android:id= "@+id/tab_txt" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android: gravity= "Center" android:text= "You"/> </LinearLayout> <linearlayout android:id= "@+id/tab" AndroId:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <TextView Android:id= "@+id/tab_txt" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" android:gravity= "Center" android:text= "/> </LinearLayout> <linearlayout android:id=" @+id/tab "android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "> <textview android:id=" @+id/ Tab_txt "android:layout_width=" fill_parent "android:layout_height=" fill_parent "" Center "Android:
text= "he"/> </LinearLayout> <linearlayout android:id= "@+id/tab" android:layout_width= "Fill_parent"
android:layout_height= "fill_parent" android:orientation= "vertical" > <textview android:id= "@+id/tab_txt" Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:gravity= "center" android:text= "US"  /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>

Second, create a layout that displays this Tabwidget tabmini.xml (located under the Res/layout directory)

<?xml version= "." Encoding= "utf-"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk/ Res/android "
android:layout_width=" fill_parent "
android:layout_height=" Wrap_content "
android: Paddingtop= "DP"
android:paddingleft= "DP"
android:paddingright= "DP"
android:background= "#CEC" >
<textview 
android:id= "@+id/tab_label"
android:layout_width= "fill_parent"
android:layout _height= "Wrap_content"
android:layout_centerinparent= "true"
android:gravity= "center"
android: Textcolor= "#"
android:textstyle= "bold"
android:background= "@drawable/tabmini"/>
</ Relativelayout>

Third, create a selector in the drawable, named Tabmini.xml, used to click on the Tabhost tab when TextView changes

<?xml version= "." Encoding= "utf-"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/ Android >
<item 
android:state_selected= "true"
android:drawable= "@drawable/add_managebg_down" /> 
<item 
android:state_selected= "false" 
android:drawable= "@drawable/add_managebg"/>

Four, Java code, in the activity to achieve tabhost

Package Com.example.androidtest____meihuatubiao;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.TabHost;
Import Android.widget.TextView; public class Main extends activity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (Savedin
Stancestate);
Setcontentview (R.layout.main);
View nitab= Layoutinflater.from (this). Inflate (R.layout.tabmini, NULL);
TextView nitxt= (TextView) Nitab.findviewbyid (R.id.tab_label);
Nitxt.settext ("ni");
View wotab= Layoutinflater.from (this). Inflate (R.layout.tabmini, NULL);
TextView wotxt= (TextView) Wotab.findviewbyid (R.id.tab_label);
Wotxt.settext ("Wo");
View tatab= Layoutinflater.from (this). Inflate (R.layout.tabmini, NULL);
TextView tatxt= (TextView) Tatab.findviewbyid (R.id.tab_label);
Tatxt.settext ("Ta");
View wetab= Layoutinflater.from (this). Inflate (R.layout.tabmini, NULL); TextView wetxt= (TextView) Wetab.findviewbyid (r.id.tab_lAbel);
Wetxt.settext ("we");
Tabhost tabs= (tabhost) Findviewbyid (r.id.tabhost);
Tabs.setup ();
Tabs.addtab (Tabs.newtabspec ("Nitab"). SetContent (R.id.tab). Setindicator (Nitab));
Tabs.addtab (Tabs.newtabspec ("Wotab"). SetContent (R.id.tab). Setindicator (Wotab));
Tabs.addtab (Tabs.newtabspec ("Tatab"). SetContent (R.id.tab). Setindicator (Tatab));
Tabs.addtab (Tabs.newtabspec ("Wetab"). SetContent (R.id.tab). Setindicator (Wetab)); }
}

The above content is small to share the Android program development of custom settings Tabhost,tabwidget style, I hope to help!

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.