Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 TabHost 實作新浪微博風格底部選單

編輯:關於 Android 程式設計

先看成品:

主佈局 main.xml

TabHost 需要三個特定 id 的子元件:@android:id/tabhost、@android:id/tabcontent、@android:id/tabs。這裡把 TabWidget 隱藏起來,改用自訂的 RadioGroup 作為底部工具列。

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- 後面的會覆蓋前面的 -->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="1.0" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2.0"
            android:visibility="gone" />

        <RadioGroup
            android:id="@+id/main_tab"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@drawable/maintab_toolbar_bg"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio_button0"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_1_n"
                android:text="@string/main_home" />
            <RadioButton
                android:id="@+id/radio_button1"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_2_n"
                android:text="@string/main_news" />
            <RadioButton
                android:id="@+id/radio_button2"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_3_n"
                android:text="@string/main_my_info" />
            <RadioButton
                android:id="@+id/radio_button3"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_4_n"
                android:text="@string/menu_search" />
            <RadioButton
                android:id="@+id/radio_button4"
                android:layout_marginTop="2.0dip"
                android:drawableTop="@drawable/icon_5_n"
                android:text="@string/more" />
        </RadioGroup>
    </LinearLayout>
</TabHost>

樣式 style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="main_tab_bottom">
        <item name="android:textSize">@dimen/bottom_tab_font_size</item>
        <item name="android:textColor">#ffffffff</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:background">@drawable/home_btn_bg</item>
        <item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@null</item>
        <item name="android:singleLine">true</item>
        <item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>
        <item name="android:layout_weight">1.0</item>
        <item name="android:layout_marginBottom">2.0dip</item>
    </style>
</resources>

其中 android:button=@null 很重要,它拿掉了 RadioButton 預設的圓點,讓版面只剩圖標與文字。

dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 圖片和文字之間距離 -->
    <dimen name="bottom_tab_padding_drawable">5.0dip</dimen>
    <!-- 和上方距離 -->
    <dimen name="bottom_tab_padding_up">3.0dip</dimen>
    <dimen name="bottom_tab_font_size">10.0dip</dimen>
</resources>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">SinaWeibo</string>
    <string name="main_home">首頁</string>
    <string name="main_news">信息</string>
    <string name="main_my_info">我的資料</string>
    <string name="menu_search">搜索</string>
    <string name="more">更多</string>
</resources>

MainTabActivity

package com.chao.demo;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

public class MainTabActivity extends TabActivity implements OnCheckedChangeListener {

    private RadioGroup mainTab;
    private TabHost tabhost;
    private Intent iHome, iNews, iInfo, iSearch, iMore;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        mainTab = (RadioGroup) findViewById(R.id.main_tab);
        mainTab.setOnCheckedChangeListener(this);

        tabhost = getTabHost();

        iHome = new Intent(this, HomeActivity.class);
        tabhost.addTab(tabhost.newTabSpec("iHome")
                .setIndicator(getResources().getString(R.string.main_home),
                              getResources().getDrawable(R.drawable.icon_1_n))
                .setContent(iHome));

        iNews = new Intent(this, NewsActivity.class);
        tabhost.addTab(tabhost.newTabSpec("iNews")
                .setIndicator(getResources().getString(R.string.main_news),
                              getResources().getDrawable(R.drawable.icon_2_n))
                .setContent(iNews));

        iInfo = new Intent(this, MyInfoActivity.class);
        tabhost.addTab(tabhost.newTabSpec("iInfo")
                .setIndicator(getResources().getString(R.string.main_my_info),
                              getResources().getDrawable(R.drawable.icon_3_n))
                .setContent(iInfo));

        iSearch = new Intent(this, SearchActivity.class);
        tabhost.addTab(tabhost.newTabSpec("iSearch")
                .setIndicator(getResources().getString(R.string.menu_search),
                              getResources().getDrawable(R.drawable.icon_4_n))
                .setContent(iSearch));

        iMore = new Intent(this, MoreActivity.class);
        tabhost.addTab(tabhost.newTabSpec("iMore")
                .setIndicator(getResources().getString(R.string.more),
                              getResources().getDrawable(R.drawable.icon_5_n))
                .setContent(iMore));
    }

    // 監聽點擊的按鈕,切換對應頁面
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radio_button0: tabhost.setCurrentTabByTag("iHome");   break;
            case R.id.radio_button1: tabhost.setCurrentTabByTag("iNews");   break;
            case R.id.radio_button2: tabhost.setCurrentTabByTag("iInfo");   break;
            case R.id.radio_button3: tabhost.setCurrentTabByTag("iSearch"); break;
            case R.id.radio_button4: tabhost.setCurrentTabByTag("iMore");   break;
        }
    }
}

HomeActivity

package com.chao.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HomeActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        textView.setText("這是首頁");
        setContentView(textView);
    }
}

其他頁面的實作方式類似,此處不再贅列。

幾個實作要點

  • tag 與 RadioButton 的對應:newTabSpec 傳入的 tag 字串就是 setCurrentTabByTag 的依據,兩邊必須一致,建議抽成常量避免打錯字。
  • TabHost 的 id 不能改:tabhost、tabcontent、tabs 這三個都是 framework 定義好的,必須用 @android:id/ 引用。
  • 記憶體成本:每個 Tab 的內容都是 Intent 對應的 Activity,切換時會各自持有 View,頁面多時應在 onCheckedChanged 中搭配懶加載或 Fragment 複用。
  • 現代替代方案:TabActivity 已標示為 deprecated,新專案建議改用 FragmentActivity + FragmentPagerAdapter,或 BottomNavigationView 搭配 Navigation Component,能少寫不少樣板程式碼,也更好維護。

完整工程:https://download.csdn.net/detail/u014071669/7187077

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved