Requirement: Use tabhost to implement the bottom menu bar;
:
Implementation Analysis:
1. directory structure:
Code implementation:
1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="2dp" android:layout_weight="0" android:background="@drawable/tab_widget_background" /> </LinearLayout></TabHost>
2 activity_one.xml
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/image_01" ></FrameLayout>
3 item_tab_view.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/image_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="3dp" /> <TextView android:id="@+id/text_name" style="@style/item_tab_text_style" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
4 tab_background_selector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/tab_item_p" android:state_pressed="true"/> <item android:drawable="@drawable/tab_item_d" android:state_selected="true"/></selector>
5 style. xml
<style name="item_tab_text_style"> <item name="android:textSize">10.0dip</item> <item name="android:textColor">#ffffffff</item> <item name="android:ellipsize">marquee</item> <item name="android:singleLine">true</item> </style>
6 constant. Java
Package COM. JJC. demo;/*** @ author ThinkPad * function description: constant tool class */public class constant {public static final class convalue {/*** tab icon */public static int mimageviewarray [] = {R. drawable. tab_icon1, R. drawable. tab_icon2, R. drawable. tab_icon3, R. drawable. tab_icon4, R. drawable. tab_icon5};/*** tab text */public static string mtextviewarray [] = {"Homepage", "about", "Settings", "Search ", "More"};/*** each tab page */PU BLIC static class <?> Mtabclassarray [] = {activityone. Class, activitytwo. Class, activitythree. Class, activityfour. Class, activityfive. Class };}}
7 activityone. Java
package com.jjc.demo;import android.app.Activity;import android.os.Bundle;public class ActivityOne extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_one); }}
8. mainactivity. Java
Package COM. JJC. demo; import COM. JJC. demo. constant. convalue; import android. app. tabactivity; import android. content. intent; import android. OS. bundle; import android. view. layoutinflater; import android. view. view; import android. widget. imageview; import android. widget. tabhost; import android. widget. tabhost. tabspec; import android. widget. textview; public class mainactivity extends tabactivity {private tabhost m Tabhost; private layoutinflater minflater; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); initview ();}/*** initialization component */private void initview () {// instantiate the tabhost object to obtain tabhost mtabhost = gettabhost (); // instantiate the layout object minflater = layoutinflater. from (this); // obtain the number of activities int COUNT = convalue. mtabclassarray. length; f Or (INT I = 0; I <count; I ++) {// set the icon, text, and content tabspec = mtabhost for each tab button. newtabspec (convalue. mtextviewarray [I]). setindicator (gettabitemview (I )). setcontent (gettabitemintent (I); // Add the tab button to the mtabhost tab. addtab (tabspec); // set the background of the tab button mtabhost. gettabwidget (). getchildat (I ). setbackgroundresource (R. drawable. tab_background_selector);}/*** set the icon and text for the tab button */private view gettabitemvie W (INT index) {view = minflater. inflate (R. layout. item_tab_view, null); imageview = (imageview) view. findviewbyid (R. id. image_icon); If (imageview! = NULL) {imageview. setimageresource (convalue. mimageviewarray [Index]);} textview = (textview) view. findviewbyid (R. id. text_name); textview. settext (convalue. mtextviewarray [Index]); Return view;}/*** set content for the tab (each content is an activity) */private intent gettabitemintent (INT index) {intent = new intent (this, convalue. mtabclassarray [Index]); Return intent ;}}
Code: http://pan.baidu.com/s/1lLFx8
Bottom menu bar (1) tabhost implementation