滑动菜单解决方案之三:使用TabHost控件实现微信界面的底部菜单效果

原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://wangcuijing.blog.51cto.com/7233352/1282140

滑动菜单解决方案之三:使用TabHost控件实现微信界面的底部菜单效果_第1张图片


首先,在布局文件中的代码如下:(菜单位于底部,需要在代码中设置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
< 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 >


其次,分别作四个菜单选项的selector文件。状态为选中和默认:

1
2
3
4
< 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 >


注意:处于选中状态的菜单,底部还会有个发光的背景,也是将背景设置为selector,代码如下:

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




再次,在代码中得到tabhost对象,并设置对应的四个不同的tab页,代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public  class  MainActivity  extends  android.app.TabActivity
{ //继承TabActivity
     String[] name = { "微信" , "通讯录" , "朋友们" , "设置" };
     //写好四个selector文件,内容为选中状态和默认状态时的图片
     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++)
         { //复用布局,创建四个View的对象,并且分别设置View对象里的控件值和图片
             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 ;
     }
}

本文出自 “wangcuijing” 博客,请务必保留此出处http://wangcuijing.blog.51cto.com/7233352/1282140

你可能感兴趣的:(activity滑动菜单)