Android學習筆記---使用TabHost實現微信底部導航欄效果


一. TabHost介紹

 

TabHost組件可以在界面中存放多個選項卡, 很多軟件都使用了改組件進行設計;

 

1. TabHost常用組件

 

TabWidget : 該組件就是TabHost標簽頁中上部 或者 下部的按鈕, 可以點擊按鈕切換選項卡;

TabSpec : 代表了選項卡界面, 添加一個TabSpec即可添加到TabHost中;

-- 創建選項卡 : newTabSpec(String tag), 創建一個選項卡;

-- 添加選項卡 : addTab(tabSpec);

 

2. TabHost使用步驟

 

a. 定義布局 : 在XML文件中使用TabHost組件, 並在其中定義一個FrameLayout選項卡內容;

b. 繼承TabActivity : 顯示選項卡組件的Activity繼承TabActivity;

c. 獲取組件 : 通過調用getTabHost()方法, 獲取TabHost對象;

d. 創建添加選項卡 : 通過TabHost創建添加選項卡;

 

3. 將按鈕放到下面

 

布局文件中TabWidget代表的就是選項卡按鈕, Fragement組件代表內容;

設置失敗情況 : 如果Fragement組件沒有設置 android:layout_weight屬性, 那么將TabWidget放到下面, 可能不會顯示按鈕;

設置權重 : 設置了Fragment組件的權重之后, 就可以成功顯示該選項卡按鈕;

 

下面上代碼先是main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:background="#FFFFFF"
 6     android:orientation="vertical" >
 7 
 8     <TabHost
 9         android:id="@android:id/tabhost"
10         android:layout_width="fill_parent"
11         android:layout_height="fill_parent" >
12 
13         <LinearLayout
14             android:layout_width="fill_parent"
15             android:layout_height="fill_parent"
16             android:orientation="vertical" >
17 
18             <FrameLayout
19                 android:id="@android:id/tabcontent"
20                 android:layout_width="fill_parent"
21                 android:layout_height="0.0dip"
22                 android:layout_weight="1.0" />
23 
24             <TabWidget
25                 android:id="@android:id/tabs"
26                 android:layout_width="fill_parent"
27                 android:layout_height="wrap_content"
28                 android:layout_weight="0.0"
29                 android:visibility="gone" />
30 
31             <FrameLayout
32                 android:layout_width="fill_parent"
33                 android:layout_height="wrap_content" >
34 
35                 <RadioGroup
36                     android:id="@+id/main_tab_group"
37                     android:layout_width="fill_parent"
38                     android:layout_height="wrap_content"
39                     android:layout_gravity="bottom"
40                     android:gravity="bottom"
41                     android:orientation="horizontal" >
42 
43                     <RadioButton
44                         android:id="@+id/guide_home"
45                         style="@style/main_tab_bottom"
46                         android:checked="true"
47                         android:drawableTop="@drawable/guide_home"
48                         android:text="首頁" />
49 
50                     <RadioButton
51                         android:id="@+id/guide_store"
52                         style="@style/main_tab_bottom"
53                         android:drawableTop="@drawable/guide_group"
54                         android:text="商城" />
55 
56                     <RadioButton
57                         android:id="@+id/guide_cart"
58                         style="@style/main_tab_bottom"
59 
60                         android:drawableTop="@drawable/guide_cart"
61                         android:text="購物車" />
62 
63                     <RadioButton
64                         android:id="@+id/guide_ww"
65                         style="@style/main_tab_bottom"
66                         android:drawableTop="@drawable/guide_setting"
67                         android:text="我的" />
68                 </RadioGroup>
69             </FrameLayout>
70         </LinearLayout>
71     </TabHost>
72 
73 </LinearLayout>

 

Activity.main.java  代碼

 1 package com.example.myapplication;
 2 
 3 import android.app.TabActivity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.RadioButton;
 8 import android.widget.TabHost;
 9 
10 public class MainActivity extends TabActivity {
11     private RadioButton guide_home, guide_store, guide_cart,guide_ww;
12     private Intent intent1;
13     private Intent intent2;
14     private Intent intent3;
15     private Intent intent4;
16     private String tab="tab0";
17     private int currIndex = 0;
18     private TabHost tabHost;
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         //初始化四個Activity
24         intent1 = new Intent(this, Main2Activity.class);
25         intent2 = new Intent(this, Main3Activity.class);
26         intent3 = new Intent(this, Main4Activity.class);
27         intent4 = new Intent(this, Main5Activity.class);
28         init();
29         inittAB();
30     }
31 
32     /**
33      * 初始化組件
34      */
35     private void init() {
36             guide_home = (RadioButton) findViewById(R.id.guide_home);
37             guide_cart = (RadioButton) findViewById(R.id.guide_cart);
38             guide_ww = (RadioButton) findViewById(R.id.guide_ww);
39             guide_store=(RadioButton) findViewById(R.id.guide_store);
40             //設置點擊事件
41             guide_home.setOnClickListener(new MyOnPageChangeListener());
42             guide_store.setOnClickListener(new MyOnPageChangeListener());
43             guide_cart.setOnClickListener(new MyOnPageChangeListener());
44             guide_ww.setOnClickListener(new MyOnPageChangeListener());
45     }
46 
47     //填充TabHost
48     private void inittAB() {
49         tabHost = getTabHost();
50         //這里tab0是第一個,tab1是第二個窗口,以此類推
51         tabHost.addTab(tabHost.newTabSpec("tab0")
52                 .setIndicator("tab0")
53                 .setContent(intent1));
54         tabHost.addTab(tabHost.newTabSpec("tab1")
55                 .setIndicator("tab1")
56                 .setContent(intent2));
57         tabHost.addTab(tabHost.newTabSpec("tab3")
58                 .setIndicator("tab3")
59                 .setContent(intent4));
60         tabHost.addTab(tabHost.newTabSpec("tab2")
61                 .setIndicator("tab2")
62                 .setContent(intent3));
63         if(tab.equalsIgnoreCase("tab0")){
64             tabHost.setCurrentTabByTag("tab0");
65         }
66     }
67 
68     /**
69      * 點擊事件類
70      */
71     private class MyOnPageChangeListener implements View.OnClickListener {
72 
73         @Override
74         public void onClick(View v) {
75             switch (v.getId()) {
76                 case R.id.guide_home:
77                     currIndex = 0;
78                     //切換第一個窗口
79                     tabHost.setCurrentTabByTag("tab0");
80                     break;
81                 case R.id.guide_cart:
82                     currIndex = 3;
83                     //切換第二個窗口
84                     tabHost.setCurrentTabByTag("tab2");
85                     break;
86                 case R.id.guide_store:
87                     currIndex = 4;
88                     //切換第三個窗口
89                     tabHost.setCurrentTabByTag("tab1");
90                     break;
91                 case R.id.guide_ww:
92                     currIndex = 5;
93                     //切換第四個窗口
94                     tabHost.setCurrentTabByTag("tab3");
95                     break;
96             }
97         }
98     }
99 }

下面是效果圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM