Use the TabHost control in android to implement the menu at the bottom of the WeChat Interface

Source: Internet
Author: User

650) this. length = 650; "src =" http://img1.51cto.com/attachment/201308/173400734.jpg "title =" qq 3020.173001.jpg "width =" 250 "height =" 362 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 250px; height: 362px; "/>


First, the code in the layout file is as follows: the menu is located at the bottom and needs to be set in the Code)

<TabHost       android:id="@android:id/tabhost"       android:layout_width="match_parent"       android:layout_height="match_parent"       android:layout_alignParentLeft="true"       android:layout_alignParentTop="true" >       <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="match_parent"               android:layout_weight="1">           </FrameLayout>             <TabWidget               android:id="@android:id/tabs"               android:layout_width="match_parent"               android:layout_height="60dp"               android:layout_weight="0"               android:background="@drawable/bottom_bar"               >           </TabWidget>       </LinearLayout>   </TabHost>


Next, create the selector file with four menu options. Status: select and default:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_selected="true" android:drawable="@drawable/tab_weixin_pressed"></item>    <item android:drawable="@drawable/tab_weixin_normal"></item></selector>


Note: In the selected menu, there will be a luminous background at the bottom, and the background is set to selector. The Code is as follows:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_selected="true" android:drawable="@drawable/tab_bg"></item></selector>




Again, get the tabhost object in the Code and set the corresponding four different tab pages. The Code is as follows:


Public class MainActivity extends android. app. tabActivity {// inherit TabActivity String [] name = {"", "Address Book", "friends", "Settings"}; // write four selector files, int [] imaResid = {R. drawable. selector_tab1, R. drawable. selector_tab2, R. drawable. selector_tab3, R. drawable. selector_tab4}; ArrayList <TabSpec> arrayList = new ArrayList <TabHost. tabSpec> (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_tab); TabHost tabHost = getTabHost (); for (int I = 0; I <4; I ++) {// reuse the layout to create four View objects, set the control value in the View object and the Image View tabrl = getLayoutInflater (). inflate (R. layout. tab_item, null); TextView TV _name = (TextView) tabrl. findViewById (R. id. TV _name); TV _name.setText (name [I]); ImageView iv_icon = (ImageView) tabrl. findViewById (R. id. iv_icon); iv_icon.setImageResource (imaResid [I]); TabSpec spec = tabHost. newTabSpec ("tab" + I ). setIndicator (tabrl); arrayList. add (spec);} tabHost. addTab (arrayList. get (0 ). setContent (new Intent (MainActivity. this, Tab1Activity. class); tabHost. addTab (arrayList. get (1 ). setContent (new Intent (MainActivity. this, Tab2Activity. class); tabHost. addTab (arrayList. get (2 ). setContent (new Intent (MainActivity. this, Tab3Activity. class); tabHost. addTab (arrayList. get (3 ). setContent (new Intent (MainActivity. this, Tab4Activity. class);} @ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. tab, menu); return true ;}}

This article is from the "wangcuijing" blog, please be sure to keep this source http://wangcuijing.blog.51cto.com/7233352/1282140

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.