編輯:關於 Android 程式設計
在實際開發中,像微信、新浪微博這類應用的主頁,底部往往會有一排可以切換的標籤欄。這篇文章就帶大家用 Fragment 搭配 FragmentTabHost 這套組合,一步步實現這樣的底部導航效果。
整體思路是:上方用 FrameLayout 作為內容承載區,底部放一條分割線,再擺放 FragmentTabHost 作為標籤容器。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:background="@color/white" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/color_home_tab_line" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/et_divider_disable"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout> </LinearLayout>
下面這段代碼負責初始化底部菜單:先準備好圖標、文字與 Fragment 三組數組,再遍歷添加每一個 Tab,最後設置默認選中第一項並去掉分隔線。
public class MainActivity { @ViewInject(android.R.id.tabhost) private FragmentTabHost mTabHost; private LayoutInflater layoutInflater; private int mImageViewArray[] = {R.drawable.home_tab1, R.drawable.home_tab2, R.drawable.home_tab3, R.drawable.home_tab4}; private String mTextviewArray[] = {"首頁", "圈子", "資訊","個人中心"}; private Class fragmentArray[] = {Fragment1.class, Fragment2.class, Fragment3.class,Fragment4.class}; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } @Override protected void init() { // list=new JSONArray(); layoutInflater=LayoutInflater.from(this); initTabHost();//初始化底部菜單 } /** * 初始化底部工具欄 */ private void initTabHost() { mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); int count = fragmentArray.length; for (int i = 0; i < count; i++) { TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]) .setIndicator(getTabItemView(i)); mTabHost.addTab(tabSpec, fragmentArray[i], null); mTabHost.getTabWidget().getChildAt(i) .setBackgroundResource(R.color.white); } mTabHost.setCurrentTabByTag(mTextviewArray[0]); mTabHost.getTabWidget().setDividerDrawable(null); } /** * 項的樣式 * @param index 第幾個 * @return 每一個Tab樣式 */ private View getTabItemView(int index) { View view = layoutInflater.inflate(R.layout.tab_home_item, null); ImageView imageView = (ImageView) view.findViewById(R.id.icon); imageView.setImageResource(mImageViewArray[index]); TextView textView = (TextView) view.findViewById(R.id.name); textView.setText(mTextviewArray[index]); return view; } }
核心要點在於 getTabItemView() 方法:它通過 LayoutInflater 加載自定義的 Tab 樣式布局,把圖標和文字填充進去,從而使每個 Tab 都能呈現「圖標 + 文字」的視覺效果。掌握了這套寫法,你就能輕鬆複製出各類主流應用的底部導航欄。
用 Vitamio 打造萬能播放器(十):本地播放之縮略圖、視頻信息與掃描服務
前言 Vitamio 是我們團隊的誠意之作。除了要把 VPlayer 打造成 Android 上最好的播放器,我們也希望把 Vitamio 打磨成最優秀的播放器組件。新版
Android網絡檢測實戰:判斷聯網狀態並引導用戶去設置
專案裡經常需要確認設備當前是否處於聯網狀態。本文記錄如何檢測網絡連接,並在離線時彈出提示框,引導使用者直接跳到系統網絡設置頁。 效果預覽 (1)聯網情況下: (2)未聯
Android 入門實戰:打造最簡單的計算器
案例目標 這個範例會建立一個最簡單的計算器。第一個頁面負責輸入兩個數字,第二個頁面負責顯示計算結果。透過這個案例,可以練習 Android 的頁面跳轉、Intent 傳值
Android 根據螢幕寬度按比例縮放圖片
ImageView 有 scaleType 屬性可以縮放圖片,讓圖片鋪滿螢幕寬度,但往往會出現壓縮或裁剪的情況。 ImageView 的 scaleType 屬性分別是