android FragmentTabhost實(shí)現(xiàn)導(dǎo)航分頁
本文實(shí)例為大家分享了android FragmentTabhost導(dǎo)航分頁展示的具體代碼,供大家參考,具體內(nèi)容如下
基本模板
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
private LayoutInflater mLayoutInflater;
private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
Fragment3.class, Fragment4.class, Fragment5.class };
private int mImageArray[] = { R.drawable.tab_home_btn,
R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,
R.drawable.tab_square_btn, R.drawable.tab_more_btn };
private String mTextArray[] = { "首頁", "消息", "好友", "搜索", "更多" };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mLayoutInflater = LayoutInflater.from(this);
// 找到TabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.getTabWidget().setDividerDrawable(null);//去除分割線
// 得到fragment的個(gè)數(shù)
for (int i = 0; i < mFragmentArray.length; i++) {
// 給每個(gè)Tab按鈕設(shè)置圖標(biāo)、文字和內(nèi)容
TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
.setIndicator(getTabItemView(i));
// 將Tab按鈕添加進(jìn)Tab選項(xiàng)卡中
mTabHost.addTab(tabSpec, mFragmentArray[i], null);
// 設(shè)置Tab按鈕的背景
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.selector_tab_background);
}
}
//給每個(gè)Tab按鈕設(shè)置圖標(biāo)和文字
private View getTabItemView(int index) {
View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
ImageView imageView = view.findViewById(R.id.imageview);
//設(shè)置圖片選擇器,選中的tab改變圖標(biāo)
switch (index){
case 0:imageView.setImageResource(R.drawable.main_bottom_image_selector);break;
case 1:imageView.setImageResource(R.drawable.main_bottom_image_selector2);break;
case 2:imageView.setImageResource(R.drawable.main_bottom_image_selector3);break;
case 3:imageView.setImageResource(R.drawable.main_bottom_image_selector4);break;
case 4:imageView.setImageResource(R.drawable.main_bottom_image_selector5);break;
}
TextView textView = view.findViewById(R.id.textview);
textView.setText(mTextArray[index]);
//設(shè)置文本選擇器,選中的tab文字高亮
textView.setTextColor(R.drawable.main_bottom_text_selector);
return view;
}
}
acitivity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/realtabcontent" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" //必須使用提供的id android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/bg_tabhost_bg"> <FrameLayout android:id="@android:id/tabcontent" //必須使用提供的id android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
tab_item_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageview"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="3dp"
/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:textSize="12sp"
android:layout_marginBottom="2dp"/>
</LinearLayout>
main_bottom_image_selector圖片選擇器
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="false" android:drawable="@drawable/ic_launcher" /> <item android:state_selected="true" android:drawable="@drawable/ic_launcher_round" /> </selector>
注:模板有5個(gè)tab,需要5個(gè)圖片選擇器,還需要5個(gè)文本選擇器,還需要?jiǎng)?chuàng)建5個(gè)fragment。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法
- Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
- Android 中 TabHost與ViewPager結(jié)合實(shí)現(xiàn)首頁導(dǎo)航效果
- Android組件必學(xué)之TabHost使用方法詳解
- Android組件TabHost實(shí)現(xiàn)頁面中多個(gè)選項(xiàng)卡切換效果
- Android程序開發(fā)之自定義設(shè)置TabHost,TabWidget樣式
- android TabHost(選項(xiàng)卡)的使用方法
- Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡
相關(guān)文章
AndroidStudio Gradle第三依賴統(tǒng)一管理的實(shí)現(xiàn)方法
這篇文章主要介紹了AndroidStudio Gradle第三依賴統(tǒng)一管理的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-09-09
android開發(fā)教程之ubuntu使用adb連接小米2的步驟和adb調(diào)試方法
這篇文章主要介紹了ubuntu中使用adb連接小米2的步驟和adb調(diào)試方法,需要的朋友可以參考下2014-02-02
詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案
這篇文章主要介紹了詳解Android_性能優(yōu)化之ViewPager加載成百上千高清大圖oom解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12
Android使用Kotlin和RxJava 2.×實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)效果
本篇文章主要介紹了Android使用Kotlin和RxJava 2.×實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)效果,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-12-12
Android實(shí)現(xiàn)APP歡迎頁面簡單制作思路
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)APP歡迎頁面簡單制作思路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android應(yīng)用創(chuàng)建多個(gè)快捷方式
本文主要介紹Android 生成多個(gè)快捷方式,這里提供代碼實(shí)例,詳細(xì)講解生成多個(gè)快捷方式的實(shí)現(xiàn)方法,有需要的朋友可以參考下2016-07-07
Android 逆向?qū)W習(xí)詳解及實(shí)例
本文主要介紹Android 逆向?qū)W習(xí),這里整理逆向?qū)W習(xí)的思路及學(xué)習(xí)要點(diǎn),并附示例代碼,幫助大家學(xué)習(xí)理解,有需要的小伙伴可以參考下2016-09-09

