Android design包自定義tablayout的底部導(dǎo)航欄的實(shí)現(xiàn)方法
以前做項(xiàng)目大多用的radiobutton,今天用tablayout來做一個(gè)tab切換頁面的的效果.
實(shí)現(xiàn)的效果就是類似QQ.微信的頁面間(也就是Fragment間)的切換.如圖:
布局只要一個(gè)tablayout
<android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/nav_tl" app:tabIndicatorHeight="0dp"http://將指示器去掉 ps:如果大家對tablayout有一些樣式上的需求 可以自定義style,這里就不加了 android:layout_gravity="bottom" ></android.support.design.widget.TabLayout>
然后就是activity了
public class MainActivity extends BaseActivity implements TabLayout.OnTabSelectedListener{ @BindView(R.id.main_container) LinearLayout mainContainer; @BindView(R.id.nav_tl) TabLayout navTl; //Tab 文字 private final int[] TAB_TITLES = new int[]{R.string.nav_home,R.string.nav_order,R.string.nav_my}; //Tab 圖片 private final int[] TAB_IMGS = new int[]{R.drawable.nav_home_bg,R.drawable.nav_order_bg,R.drawable.nav_my_bg}; //貼出一個(gè)R.drawable.nav_home_bg的文件,其他類似:`<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@mipmap/nav_home_selected"></item> <item android:state_selected="false" android:drawable="@mipmap/nav_home_normal"></item> </selector>` private FirstPagerFragment firstPagerFragment; private PersonalFragment personalFragment; private SeekOrderFragment seekOrderFragment; private android.support.v4.app.FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); iniView(); iniData(); } private void iniData() { } private void iniView() { LayoutInflater inflater = LayoutInflater.from(this); manager = getSupportFragmentManager(); getTab(R.id.main_container,manager,0); setTabs(navTl,inflater,TAB_TITLES,TAB_IMGS); navTl.setOnTabSelectedListener(this); } /** * @description: 設(shè)置添加Tab * 我們自定義的布局customer_layout其實(shí)就是一張圖片加文字 * `<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/img_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView android:id="@+id/tv_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="@drawable/tv_color_bg"http://text我用了一個(gè)顏色選擇器,主要在我選中的時(shí)候能改變顏色 /> </LinearLayout>` */ private void setTabs(TabLayout tabLayout, LayoutInflater inflater, int[] tabTitlees, int[] tabImgs) { for (int i = 0; i < tabImgs.length; i++) { TabLayout.Tab tab = tabLayout.newTab(); View view = inflater.inflate(R.layout.customer_layout, null); tab.setCustomView(view); TextView tvTitle = (TextView) view.findViewById(R.id.tv_tab); tvTitle.setText(tabTitlees[i]); ImageView imgTab = (ImageView) view.findViewById(R.id.img_tab); imgTab.setImageResource(tabImgs[i]); tabLayout.addTab(tab); } } @Override public void onTabSelected(TabLayout.Tab tab) { getTab(R.id.main_container,manager,tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } //設(shè)置tab的監(jiān)聽,選中某一個(gè)tab對應(yīng)的Fragment要及時(shí)切換,相信大家看代碼能看明白 private void getTab(int container, android.support.v4.app.FragmentManager manager, int position){ FragmentTransaction ft = manager.beginTransaction(); hideAll(ft); switch (position){ case 0: if(firstPagerFragment==null){ firstPagerFragment=new FirstPagerFragment(); ft.add(container,firstPagerFragment); }else { ft.show(firstPagerFragment); } break; case 1: if(seekOrderFragment==null){ seekOrderFragment=new SeekOrderFragment(); ft.add(container,seekOrderFragment); }else { ft.show(seekOrderFragment); } break; case 2: if(personalFragment==null){ personalFragment=new PersonalFragment(); ft.add(container,personalFragment); }else { ft.show(personalFragment); } break; } ft.commit(); } private void hideAll(FragmentTransaction ft) { if(firstPagerFragment!=null){ ft.hide(firstPagerFragment); } if(personalFragment!=null){ ft.hide(personalFragment); } if(seekOrderFragment!=null){ ft.hide(seekOrderFragment); } } }
以上所述是小編給大家介紹的Android design包自定義tablayout的底部導(dǎo)航欄的實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android程序開發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼
- Android實(shí)現(xiàn)底部導(dǎo)航欄功能(選項(xiàng)卡)
- 超簡單的幾行代碼搞定Android底部導(dǎo)航欄功能
- Android實(shí)現(xiàn)簡單底部導(dǎo)航欄 Android仿微信滑動(dòng)切換效果
- android中Fragment+RadioButton實(shí)現(xiàn)底部導(dǎo)航欄
- 性能分析:指如何快速定位SQL問題
- Android用Scroller實(shí)現(xiàn)一個(gè)可向上滑動(dòng)的底部導(dǎo)航欄
- ANDROID BottomNavigationBar底部導(dǎo)航欄的實(shí)現(xiàn)示例
- Android使用RadioGroup實(shí)現(xiàn)底部導(dǎo)航欄
- Android底部導(dǎo)航欄的動(dòng)態(tài)替換方案
相關(guān)文章
Android 中TabLayout自定義選擇背景滑塊的實(shí)例代碼
TabLayout是Android 的Material Design包中的一個(gè)控件,可以和V4包中的ViewPager搭配產(chǎn)生一個(gè)聯(lián)動(dòng)的效果。接下來通過本文給大家分享TabLayout自定義選擇背景滑塊的實(shí)例代碼,感興趣的朋友一起學(xué)習(xí)吧2016-10-10在Flutter中讓文字Text換行的實(shí)現(xiàn)步驟
在Flutter中,Text小部件默認(rèn)會(huì)嘗試在其父小部件的約束內(nèi)顯示所有文本內(nèi)容,如果父小部件沒有提供足夠的空間或沒有設(shè)置約束限制,Text小部件可能無法正確換行,本文給大家介紹了在Flutter中如何讓文字Text換行,需要的朋友可以參考下2024-07-07Android開發(fā)Jetpack組件Room用例講解
這篇文章主要為大家介紹了Android?Jetpack組件Room的使用案例的詳細(xì)講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02Android開發(fā)中MotionEvent坐標(biāo)獲取方法分析
這篇文章主要介紹了Android開發(fā)中MotionEvent坐標(biāo)獲取方法,結(jié)合實(shí)例形式分析了MotionEvent獲取坐標(biāo)的相關(guān)函數(shù)使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02Android自定義View實(shí)現(xiàn)五子棋小游戲
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android中微信搶紅包助手的實(shí)現(xiàn)詳解
本篇文章主要介紹了Android中微信搶紅包助手的實(shí)現(xiàn)詳解,通過利用AccessibilityService輔助服務(wù),監(jiān)測屏幕內(nèi)容,如監(jiān)聽狀態(tài)欄的信息,屏幕跳轉(zhuǎn)等,以此來實(shí)現(xiàn)自動(dòng)拆紅包的功能,有興趣的可以了解一下。2017-02-02Android ViewPager制作新手導(dǎo)航頁(動(dòng)態(tài)加載)
這篇文章主要為大家詳細(xì)介紹了Android ViewPager制作新手導(dǎo)航頁,了解什么是動(dòng)態(tài)加載指示器,感興趣的小伙伴們可以參考一下2016-05-05