[关闭]
@coder-pig 2015-08-30T14:31:27.000000Z 字数 12053 阅读 2246

Android基础入门教程——5.2.3 Fragment实例精讲——底部导航栏的实现(方法3)

Android基础入门教程


本节引言

前面我们已经跟大家讲解了实现底部导航栏的两种方案,但是这两种方案只适合普通的情况,如果
是像新浪微博那样的,想在底部导航栏上的item带有一个红色的小点,然后加上一个消息数目这样,
前面两种方案就显得无力了,我们来看看别人的APP是怎么做的,打开手机的开发者选项,勾选里面的:
显示布局边界,然后打开我们参考的那个App,可以看到底部导航栏是这样的:


从上面这个图我们就可以看出,这种底部导航栏不是简单的TextView或者RadioGroup构成的,
大概布局方案可能是:外层一个LinearLayout,中间一个RelativeLayout,而在中间有一个TextView,
然后再在TextView的右上角有一个红色圆圈背景的TextView或者一个红色的小点;
大概就这样,而这些小点平时的时候应该设置的不可见,当收到信息推送,即有相关类别信息的
时候再可见,并且显示对应的信息数目!那么下面我们就来实现下这种底部导航栏的效果,
另外,为了方便演示,这里就不演示Fragment的切换效果了!另外顺道复习下Fragment获得Activity
中的组件的知识点!


1.实现效果图:

为了方便理解,这里通过点击按钮的形式,模拟收到推送信息,然后显示红色点!

运行效果图:

2.实现流程:

好的,接下来我们就来实现上面这个效果~

Step 1:相关资源文件的准备:

和前面一样,准备好drawable系列的资源:

文字资源:tab_menu_text.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:color="@color/text_yellow" android:state_selected="true" />
  4. <item android:color="@color/text_gray" />
  5. </selector>

图标资源:tab_menu_better.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@mipmap/tab_better_pressed" android:state_selected="true" />
  4. <item android:drawable="@mipmap/tab_better_normal" />
  5. </selector>

照着把其他三个也撸出来~!


Step 2:编写activity的布局代码:

因为四个选项的TextView以及右上角的红点数字属性都差不多,如下:

  1. <TextView
  2. android:id="@+id/tab_menu_channel"
  3. android:layout_marginTop="5dp"
  4. android:layout_width="wrap_content"
  5. android:layout_height="48dp"
  6. android:layout_centerInParent="true"
  7. android:textColor="@drawable/tab_menu_text"
  8. android:drawableTop="@drawable/tab_menu_channel"
  9. android:text="@string/tab_menu_alert"/>
  10. <TextView
  11. android:layout_width="20dp"
  12. android:layout_height="20dp"
  13. android:background="@mipmap/bg_num"
  14. android:layout_toRightOf="@+id/tab_menu_channel"
  15. android:layout_marginLeft="-10dp"
  16. android:text="99+"
  17. android:textSize="12sp"
  18. android:gravity="center"
  19. android:textColor="@color/text_white"/>

我们将他们抽取出来,写到style.xml里:

  1. <style name="tab_menu_text">
  2. <item name="android:layout_marginTop">5dp</item>
  3. <item name="android:layout_width">wrap_content</item>
  4. <item name="android:layout_height">48dp</item>
  5. <item name="android:layout_centerInParent">true</item>
  6. <item name="android:textColor">@drawable/tab_menu_text</item>
  7. </style>
  8. <style name="tab_menu_bgnum">
  9. <item name="android:layout_width">20dp</item>
  10. <item name="android:layout_height">20dp</item>
  11. <item name="android:background">@mipmap/bg_num</item>
  12. <item name="android:layout_marginLeft">-10dp</item>
  13. <item name="android:textSize">12sp</item>
  14. <item name="android:gravity">center</item>
  15. <item name="android:textColor">@color/text_white</item>
  16. </style>

然后开始编写我们的activity.xml布局:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity">
  6. <RelativeLayout
  7. android:id="@+id/ly_top_bar"
  8. android:layout_width="match_parent"
  9. android:layout_height="48dp"
  10. android:background="@color/bg_topbar">
  11. <TextView
  12. android:id="@+id/txt_topbar"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:layout_centerInParent="true"
  16. android:gravity="center"
  17. android:text="信息"
  18. android:textColor="@color/text_topbar"
  19. android:textSize="18sp" />
  20. <View
  21. android:layout_width="match_parent"
  22. android:layout_height="2px"
  23. android:layout_alignParentBottom="true"
  24. android:background="@color/div_white" />
  25. </RelativeLayout>
  26. <LinearLayout
  27. android:id="@+id/ly_tab_menu"
  28. android:layout_width="match_parent"
  29. android:layout_height="56dp"
  30. android:layout_alignParentBottom="true"
  31. android:background="@color/bg_white"
  32. android:orientation="horizontal">
  33. <LinearLayout
  34. android:id="@+id/ly_tab_menu_channel"
  35. android:layout_width="0dp"
  36. android:layout_height="match_parent"
  37. android:layout_weight="1"
  38. android:gravity="center">
  39. <RelativeLayout
  40. android:layout_width="wrap_content"
  41. android:layout_height="match_parent"
  42. android:padding="5dp">
  43. <TextView
  44. android:id="@+id/tab_menu_channel"
  45. style="@style/tab_menu_text"
  46. android:drawableTop="@drawable/tab_menu_channel"
  47. android:text="@string/tab_menu_alert" />
  48. <TextView
  49. android:id="@+id/tab_menu_channel_num"
  50. style="@style/tab_menu_bgnum"
  51. android:layout_toRightOf="@+id/tab_menu_channel"
  52. android:text="99+"
  53. android:visibility="gone" />
  54. </RelativeLayout>
  55. </LinearLayout>
  56. <LinearLayout
  57. android:id="@+id/ly_tab_menu_message"
  58. android:layout_width="0dp"
  59. android:layout_height="match_parent"
  60. android:layout_weight="1"
  61. android:gravity="center">
  62. <RelativeLayout
  63. android:layout_width="wrap_content"
  64. android:layout_height="match_parent"
  65. android:padding="5dp">
  66. <TextView
  67. android:id="@+id/tab_menu_message"
  68. style="@style/tab_menu_text"
  69. android:drawableTop="@drawable/tab_menu_message"
  70. android:text="@string/tab_menu_profile" />
  71. <TextView
  72. android:id="@+id/tab_menu_message_num"
  73. style="@style/tab_menu_bgnum"
  74. android:layout_toRightOf="@+id/tab_menu_message"
  75. android:text="99+"
  76. android:visibility="gone" />
  77. </RelativeLayout>
  78. </LinearLayout>
  79. <LinearLayout
  80. android:id="@+id/ly_tab_menu_better"
  81. android:layout_width="0dp"
  82. android:layout_height="match_parent"
  83. android:layout_weight="1"
  84. android:gravity="center">
  85. <RelativeLayout
  86. android:layout_width="wrap_content"
  87. android:layout_height="match_parent"
  88. android:padding="5dp">
  89. <TextView
  90. android:id="@+id/tab_menu_better"
  91. style="@style/tab_menu_text"
  92. android:drawableTop="@drawable/tab_menu_better"
  93. android:text="@string/tab_menu_pay" />
  94. <TextView
  95. android:id="@+id/tab_menu_better_num"
  96. style="@style/tab_menu_bgnum"
  97. android:layout_toRightOf="@+id/tab_menu_better"
  98. android:text="99+"
  99. android:visibility="gone" />
  100. </RelativeLayout>
  101. </LinearLayout>
  102. <LinearLayout
  103. android:id="@+id/ly_tab_menu_setting"
  104. android:layout_width="0dp"
  105. android:layout_height="match_parent"
  106. android:layout_weight="1"
  107. android:gravity="center">
  108. <RelativeLayout
  109. android:layout_width="wrap_content"
  110. android:layout_height="match_parent"
  111. android:padding="5dp">
  112. <TextView
  113. android:id="@+id/tab_menu_setting"
  114. style="@style/tab_menu_text"
  115. android:drawableTop="@drawable/tab_menu_setting"
  116. android:text="@string/tab_menu_alert" />
  117. <ImageView
  118. android:id="@+id/tab_menu_setting_partner"
  119. android:layout_width="12dp"
  120. android:layout_height="12dp"
  121. android:layout_marginLeft="-5dp"
  122. android:layout_toRightOf="@id/tab_menu_setting"
  123. android:visibility="gone"
  124. android:src="@mipmap/partner_red" />
  125. </RelativeLayout>
  126. </LinearLayout>
  127. </LinearLayout>
  128. <View
  129. android:id="@+id/div_tab_bar"
  130. android:layout_width="match_parent"
  131. android:layout_height="2px"
  132. android:layout_above="@id/ly_tab_menu"
  133. android:background="@color/div_white" />
  134. <FrameLayout
  135. android:id="@+id/ly_content"
  136. android:layout_width="match_parent"
  137. android:layout_height="match_parent"
  138. android:layout_above="@id/div_tab_bar"
  139. android:layout_below="@id/ly_top_bar"/>
  140. </RelativeLayout>

Step 3:编写Fragment界面布局以及类

Fragment布局由四个普通按钮构成:

fg_my.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:padding="5dp">
  7. <Button
  8. android:id="@+id/btn_one"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="第一个显示信息"/>
  12. <Button
  13. android:id="@+id/btn_two"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:text="第二个显示信息"/>
  17. <Button
  18. android:id="@+id/btn_three"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:text="第三个显示信息"/>
  22. <Button
  23. android:id="@+id/btn_four"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:text="第四个显示信息"/>
  27. </LinearLayout>

接着是自定义的Fragment类,这里的话我们通过getActivity.findViewById()来获得Activity
中的小红点,这里仅仅是简单的控制显示而已!
MyFragment.java:

  1. /**
  2. * Created by Coder-pig on 2015/8/30 0030.
  3. */
  4. public class MyFragment extends Fragment implements View.OnClickListener{
  5. private Context mContext;
  6. private Button btn_one;
  7. private Button btn_two;
  8. private Button btn_three;
  9. private Button btn_four;
  10. public MyFragment() {
  11. }
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  14. View view = inflater.inflate(R.layout.fg_my,container,false);
  15. //UI Object
  16. btn_one = (Button) view.findViewById(R.id.btn_one);
  17. btn_two = (Button) view.findViewById(R.id.btn_two);
  18. btn_three = (Button) view.findViewById(R.id.btn_three);
  19. btn_four = (Button) view.findViewById(R.id.btn_four);
  20. //Bind Event
  21. btn_one.setOnClickListener(this);
  22. btn_two.setOnClickListener(this);
  23. btn_three.setOnClickListener(this);
  24. btn_four.setOnClickListener(this);
  25. return view;
  26. }
  27. @Override
  28. public void onClick(View v) {
  29. switch (v.getId()){
  30. case R.id.btn_one:
  31. TextView tab_menu_channel_num = (TextView) getActivity ().findViewById(R.id.tab_menu_channel_num);
  32. tab_menu_channel_num.setText("11");
  33. tab_menu_channel_num.setVisibility(View.VISIBLE);
  34. break;
  35. case R.id.btn_two:
  36. TextView tab_menu_message_num = (TextView) getActivity ().findViewById(R.id.tab_menu_message_num);
  37. tab_menu_message_num.setText("20");
  38. tab_menu_message_num.setVisibility(View.VISIBLE);
  39. break;
  40. case R.id.btn_three:
  41. TextView tab_menu_better_num = (TextView) getActivity ().findViewById(R.id.tab_menu_better_num);
  42. tab_menu_better_num.setText("99+");
  43. tab_menu_better_num.setVisibility(View.VISIBLE);
  44. break;
  45. case R.id.btn_four:
  46. ImageView tab_menu_setting_partner = (ImageView) getActivity ().findViewById(R.id.tab_menu_setting_partner);
  47. tab_menu_setting_partner.setVisibility(View.VISIBLE);
  48. break;
  49. }
  50. }
  51. }

Step 4:编写MainActivity

我们在这里完成主要的逻辑实现,有些部分和前面TextView实现底部导航栏的效果类似,
就不具体讲解了,代码如下:

MainActivity.java

  1. /**
  2. * Created by Coder-pig on 2015/8/30 0030.
  3. */
  4. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  5. //Activity UI Object
  6. private LinearLayout ly_tab_menu_channel;
  7. private TextView tab_menu_channel;
  8. private TextView tab_menu_channel_num;
  9. private LinearLayout ly_tab_menu_message;
  10. private TextView tab_menu_message;
  11. private TextView tab_menu_message_num;
  12. private LinearLayout ly_tab_menu_better;
  13. private TextView tab_menu_better;
  14. private TextView tab_menu_better_num;
  15. private LinearLayout ly_tab_menu_setting;
  16. private TextView tab_menu_setting;
  17. private ImageView tab_menu_setting_partner;
  18. private FragmentManager fManager;
  19. private FragmentTransaction fTransaction;
  20. private MyFragment fg1;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. bindViews();
  26. ly_tab_menu_channel.performClick();
  27. fg1 = new MyFragment();
  28. fManager = getFragmentManager();
  29. fTransaction = fManager.beginTransaction();
  30. fTransaction.add(R.id.ly_content, fg1).commit();
  31. }
  32. private void bindViews() {
  33. ly_tab_menu_channel = (LinearLayout) findViewById(R.id.ly_tab_menu_channel);
  34. tab_menu_channel = (TextView) findViewById(R.id.tab_menu_channel);
  35. tab_menu_channel_num = (TextView) findViewById(R.id.tab_menu_channel_num);
  36. ly_tab_menu_message = (LinearLayout) findViewById(R.id.ly_tab_menu_message);
  37. tab_menu_message = (TextView) findViewById(R.id.tab_menu_message);
  38. tab_menu_message_num = (TextView) findViewById(R.id.tab_menu_message_num);
  39. ly_tab_menu_better = (LinearLayout) findViewById(R.id.ly_tab_menu_better);
  40. tab_menu_better = (TextView) findViewById(R.id.tab_menu_better);
  41. tab_menu_better_num = (TextView) findViewById(R.id.tab_menu_better_num);
  42. ly_tab_menu_setting = (LinearLayout) findViewById(R.id.ly_tab_menu_setting);
  43. tab_menu_setting = (TextView) findViewById(R.id.tab_menu_setting);
  44. tab_menu_setting_partner = (ImageView) findViewById(R.id.tab_menu_setting_partner);
  45. ly_tab_menu_channel.setOnClickListener(this);
  46. ly_tab_menu_message.setOnClickListener(this);
  47. ly_tab_menu_better.setOnClickListener(this);
  48. ly_tab_menu_setting.setOnClickListener(this);
  49. }
  50. @Override
  51. public void onClick(View v) {
  52. switch (v.getId()) {
  53. case R.id.ly_tab_menu_channel:
  54. setSelected();
  55. tab_menu_channel.setSelected(true);
  56. tab_menu_channel_num.setVisibility(View.INVISIBLE);
  57. break;
  58. case R.id.ly_tab_menu_message:
  59. setSelected();
  60. tab_menu_message.setSelected(true);
  61. tab_menu_message_num.setVisibility(View.INVISIBLE);
  62. break;
  63. case R.id.ly_tab_menu_better:
  64. setSelected();
  65. tab_menu_better.setSelected(true);
  66. tab_menu_better_num.setVisibility(View.INVISIBLE);
  67. break;
  68. case R.id.ly_tab_menu_setting:
  69. setSelected();
  70. tab_menu_setting.setSelected(true);
  71. tab_menu_setting_partner.setVisibility(View.INVISIBLE);
  72. break;
  73. }
  74. }
  75. //重置所有文本的选中状态
  76. private void setSelected() {
  77. tab_menu_channel.setSelected(false);
  78. tab_menu_message.setSelected(false);
  79. tab_menu_better.setSelected(false);
  80. tab_menu_setting.setSelected(false);
  81. }
  82. }

好的,至此,就大功告成了~


3.代码下载:

FragmentDemo3.ziphttp://pan.baidu.com/s/1hqAPYFq


4.本节小结:

好的,本节相比前面两节稍微复杂了一点,不过还是比较容易弄懂的!
另外,关于实现普通底部导航栏的实现例子就写这么多吧,下一节开始我们来写下
在此基础上的根据手势操作切换页面的例子,嗯,就说这么多,谢谢~

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注