Android bottom navigation bar implementation (Part 3) TextView+LinearLayout

Android bottom navigation bar implementation (Part 3) TextView+LinearLayout

Here is a brief record of implementing the Android bottom navigation bar through TextView+LinearLayout+Fragment.

layout

  1. <! --fragment_text_tab.xml-->  
  2.  
  3. <?xml version= "1.0" encoding= "utf-8" ?>
  4. <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  5. android:layout_width= "match_parent"  
  6. android:layout_height= "match_parent"  
  7. android:orientation= "vertical" >
  8.  
  9. <FrameLayout
  10. android:id= "@+id/sub_content"  
  11. android:layout_width= "match_parent"  
  12. android:layout_height= "0dp"  
  13. android:layout_weight= "1" >
  14.  
  15. <TextView
  16. android:id= "@+id/activity_text_view"  
  17. android:layout_width= "wrap_content"  
  18. android:layout_height= "wrap_content"  
  19. android:layout_centerHorizontal= "true"  
  20. android:text= "@string/tips"  
  21. android:textColor= "@color/colorPrimary"  
  22. android:textSize= "18sp"  
  23. android:textStyle= "bold|italic" />
  24. </FrameLayout>
  25.  
  26. < View  
  27. android:layout_width= "match_parent"  
  28. android:layout_height= "1px"  
  29. android:background= "@color/grey_300" />
  30.  
  31. <include layout= "@layout/tab_layout_for_bottom" />
  32.  
  33. </LinearLayout>
  34.  
  35.  
  36.  
  37. <! --tab_layout_for_bottom-->  
  38.  
  39. <?xml version= "1.0" encoding= "utf-8" ?>
  40.  
  41.  
  42. <LinearLayout
  43. xmlns:android= "http://schemas.android.com/apk/res/android"  
  44. xmlns:tools= "http://schemas.android.com/tools"  
  45. android:layout_width= "match_parent"  
  46. android:layout_height= "56dp"  
  47. android:background= "@color/white"  
  48. android:orientation= "horizontal"  
  49. tools:showIn= "@layout/fragment_text_tab" >
  50.  
  51. <TextView
  52. android:id= "@+id/tv_home"  
  53. style= "@style/viewpager_navigation_bar_tab_style"  
  54. android:drawableTop= "@drawable/home"  
  55. android:text= "@string/item_home" />
  56.  
  57. <TextView
  58. android:id= "@+id/tv_location"  
  59. style= "@style/viewpager_navigation_bar_tab_style"  
  60. android:drawableTop= "@drawable/location"  
  61. android:text= "@string/item_location" />
  62.  
  63. <TextView
  64. android:id= "@+id/tv_like"  
  65. style= "@style/viewpager_navigation_bar_tab_style"  
  66. android:drawableTop= "@drawable/like"  
  67. android:text= "@string/item_like" />
  68.  
  69. <TextView
  70. android:id= "@+id/tv_person"  
  71. style= "@style/viewpager_navigation_bar_tab_style"  
  72. android:drawableTop= "@drawable/person"  
  73. android:text= "@string/item_person" />
  74. </LinearLayout>

Code

  1. mTHome.setOnClickListener(this);
  2. mTLocation.setOnClickListener(this);
  3. mTLike.setOnClickListener(this);
  4. mTMe.setOnClickListener(this);
  5. setDefaultFragment(); //Set the default display Fragment
  6.          
  7. @Override
  8. public void onClick( View   view ) {
  9. resetTabState();//reset the tab state
  10. switch ( view .getId()) {
  11. case R.id.tv_home:
  12. setTabState(mTHome, R.drawable.home_fill, getColor(R.color.colorPrimary)); //Set Tab state
  13. switchFrgment(0);//Switch Fragment
  14. break;
  15. case R.id.tv_location:
  16. setTabState(mTLocation, R.drawable.location_fill, getColor(R.color.colorPrimary));
  17. switchFrgment(1);
  18. break;
  19. case R.id.tv_like:
  20. setTabState(mTLike, R.drawable.like_fill, getColor(R.color.colorPrimary));
  21. switchFrgment(2);
  22. break;
  23. case R.id.tv_person:
  24. setTabState(mTMe, R.drawable.person_fill, getColor(R.color.colorPrimary));
  25. switchFrgment(3);
  26. break;
  27. }
  28. }

Fragment switching

  1. /**
  2. * switch the fragment accordting to id
  3. * @param i id
  4. */
  5. private void switchFrgment( int i) {
  6. FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
  7. switch (i) {
  8. case 0:
  9. if (mHomeFragment == null ) {
  10. mHomeFragment = mHomeFragment.newInstance(getString(R.string.item_home));
  11. }
  12. transaction .replace ( R.id.sub_content , mHomeFragment);
  13. break;
  14. case 1:
  15. if (mLocationFragment == null ) {
  16. mLocationFragment = LocationFragment.newInstance(getString(R.string.item_location));
  17. }
  18. transaction .replace ( R.id.sub_content , mLocationFragment);
  19. break;
  20. case 2:
  21. if (mLikeFragment == null ) {
  22. mLikeFragment = LikeFragment.newInstance(getString(R.string.item_like));
  23. }
  24. transaction .replace ( R.id.sub_content , mLikeFragment);
  25. break;
  26. case 3:
  27. if (mPersonFragment == null ) {
  28. mPersonFragment = PersonFragment.newInstance(getString(R.string.item_person));
  29. }
  30. transaction .replace ( R.id.sub_content , mPersonFragment);
  31. break;
  32. }
  33. transaction . commit ();
  34. }

What is worth noting here is that getChildFragmentManager() must be used, otherwise the content of the switched Fragment will not be displayed.

Set Tab State

  1. /**
  2. * set the tab state of bottom navigation bar
  3. *
  4. * @param textView the text to be shown
  5. * @param image the image
  6. * @param color the text color
  7. */
  8. private void setTabState(TextView textView, int image, int color) {
  9. textView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, image, 0, 0);//Call requires API level 17
  10. textView.setTextColor(color);
  11. }
  12.  
  13.  
  14.  
  15. /**
  16. * revert the image color and text color to black
  17. */
  18. private void resetTabState() {
  19. setTabState(mTHome, R.drawable.home, getColor(R.color.black_1));
  20. setTabState(mTLocation, R.drawable.location, getColor(R.color.black_1));
  21. setTabState(mTLike, R.drawable. like , getColor(R.color.black_1));
  22. setTabState(mTMe, R.drawable.person, getColor(R.color.black_1));
  23.  
  24. }

Note: These articles do not have too much text description, because these things are not very difficult, and they are commonly used. I believe that many people are familiar with them. It is nonsense to say more. It is clearer to read the code directly.

<<:  Android bottom navigation bar implementation (Part 2) RadioGroup

>>:  Android bottom navigation bar implementation (IV) TabLayout+ViewPager

Recommend

Interpretation of APP advertiser behavior big data in 2017

The data shows that compared with the historical ...

BlackBerry CEO: It's unfair that Apple doesn't develop apps for us

[[126825]] BlackBerry CEO John Chen took a differ...

Xiaohongshu's popular articles routines and rules

The rare popular articles with tens of millions o...

Today is the Winter Solstice | Should we eat dumplings or glutinous rice balls?

In your hometown Should we eat dumplings or gluti...

4 super practical techniques to attract traffic and increase followers!

The so-called traffic diversion means that there ...

Who controls the fate of Didi Chuxing?

A turbulent undercurrent set off by "Didi Pr...

Why traffic products are bound to become mediocre

Nowadays, many products are labeled as internet c...

The matching method of Baidu SEM promotion keywords has been upgraded!

What is keyword matching? Just like Cupid’s arrow ...

Short video operation and production skills!

With the continuous changes in various mechanisms...

Where is the future of car-sharing amid the tide of financing?

In February, the car-sharing brand "PonyCar&...

How to write To B copy? Share 2 tips

An excellent copywriter can be both cute and coqu...

Brand Innovation Theory: Social Marketing

Although the ladder (model) of the "new comm...