当前位置:   article > 正文

实现NavigationBar的两种方式_itype_extra_navigation_bar

itype_extra_navigation_bar

方式1:使用BottomNavigationView控件

xml代码:

  1. <!--底部导航栏-->
  2. <com.google.android.material.bottomnavigation.BottomNavigationView
  3. android:id="@+id/main_navigation_bar"
  4. android:layout_width="match_parent"
  5. android:layout_height="49dp"
  6. android:layout_alignParentBottom="true"
  7. app:itemIconTint="@drawable/navigation_bar_bg"
  8. app:itemTextColor="@drawable/navigation_bar_bg"
  9. android:background="@color/white"
  10. app:menu="@menu/my_navigation_items"/>

需要在menu菜单中配置每个item

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item
  4. android:id="@+id/home"
  5. android:icon="@mipmap/home_normal"
  6. android:title="@string/text_home" />
  7. <item
  8. android:id="@+id/selected"
  9. android:icon="@mipmap/select_normal"
  10. android:title="@string/text_selected" />
  11. <item
  12. android:id="@+id/red_packet"
  13. android:icon="@mipmap/red_packet_normal"
  14. android:title="@string/text_packet" />
  15. <item
  16. android:id="@+id/search"
  17. android:icon="@mipmap/search_normal"
  18. android:title="@string/text_search" />
  19. </menu>

java代码中获取对应的id即可处理点击事件:

  1. private void initListener() {
  2. mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
  3. @Override
  4. public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
  5. int itemId = item.getItemId();
  6. switch (itemId) {
  7. case R.id.home:
  8. switchFragment(mHomePageFragment);
  9. Logit.d(TAG, " cfx " + item.getTitle());
  10. break;
  11. case R.id.selected:
  12. switchFragment(mSelectedPageFragment);
  13. Logit.d(TAG, " cfx " + item.getTitle());
  14. break;
  15. case R.id.red_packet:
  16. switchFragment(mRedPacketPageFragment);
  17. Logit.d(TAG, " cfx " + item.getTitle());
  18. break;
  19. case R.id.search:
  20. switchFragment(mSearchPageFragment);
  21. Logit.d(TAG, " cfx " + item.getTitle());
  22. break;
  23. }
  24. return true;
  25. }
  26. });
  27. }

方式2:通过自定义RadioGroup实现

xml布局

  1. <!--RadioGroup继承子LinearLayout,需要设置orientation-->
  2. <RadioGroup
  3. android:id="@+id/test_navigation_bar"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:layout_alignParentBottom="true"
  7. android:orientation="horizontal">
  8. <!--android:background="@null" 没有了默认点击的阴影效果-->
  9. <!--android:button="@null" 取消RadioButton前面的选择框-->
  10. <RadioButton
  11. android:id="@+id/test_home"
  12. android:layout_width="0dp"
  13. android:layout_height="wrap_content"
  14. android:layout_weight="1"
  15. android:background="@null"
  16. android:button="@null"
  17. android:drawableTop="@drawable/test_home_selector"
  18. android:gravity="center"
  19. android:text="@string/text_home"
  20. android:textColor="@drawable/navigation_bar_bg"
  21. android:textSize="18sp" />
  22. <RadioButton
  23. android:id="@+id/test_selected"
  24. android:layout_width="0dp"
  25. android:layout_height="wrap_content"
  26. android:layout_weight="1"
  27. android:button="@null"
  28. android:drawableTop="@drawable/test_selected_selector"
  29. android:gravity="center"
  30. android:text="@string/text_selected"
  31. android:textColor="@drawable/navigation_bar_bg"
  32. android:textSize="18sp" />
  33. <RadioButton
  34. android:id="@+id/test_red_packet"
  35. android:layout_width="0dp"
  36. android:layout_height="wrap_content"
  37. android:layout_weight="1"
  38. android:button="@null"
  39. android:drawableTop="@drawable/test_packet_selector"
  40. android:gravity="center"
  41. android:text="@string/text_packet"
  42. android:textColor="@drawable/navigation_bar_bg"
  43. android:textSize="18sp" />
  44. <RadioButton
  45. android:id="@+id/test_search"
  46. android:layout_width="0dp"
  47. android:layout_height="wrap_content"
  48. android:layout_weight="1"
  49. android:button="@null"
  50. android:drawableTop="@drawable/test_search_selector"
  51. android:gravity="center"
  52. android:text="@string/text_search"
  53. android:textColor="@drawable/navigation_bar_bg"
  54. android:textSize="18sp" />
  55. </RadioGroup>
  56. <View
  57. android:layout_width="match_parent"
  58. android:layout_height="1dp"
  59. android:layout_above="@+id/test_navigation_bar"
  60. android:layout_marginBottom="20dp"
  61. android:background="@color/colorGrey" />

java代码找到相应的控件:

  1. public class TextNavigationBarActivity extends AppCompatActivity {
  2. @BindView(R.id.test_toast)
  3. public Button mTestToast;
  4. @BindView(R.id.test_navigation_bar)
  5. public RadioGroup mNavigationBar;
  6. private HomePageFragment mHomePageFragment;
  7. private SelectedPageFragment mSelectedPageFragment;
  8. private RedPacketPageFragment mRedPacketPageFragment;
  9. private SearchPageFragment mSearchPageFragment;
  10. private FragmentManager mFragmentManager;
  11. @Override
  12. protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_text_navigation_bar);
  15. ButterKnife.bind(this);
  16. initFragment();
  17. initListener();
  18. switchFragment(mHomePageFragment);
  19. }
  20. private void initFragment() {
  21. mHomePageFragment = new HomePageFragment();
  22. mSelectedPageFragment = new SelectedPageFragment();
  23. mRedPacketPageFragment = new RedPacketPageFragment();
  24. mSearchPageFragment = new SearchPageFragment();
  25. mFragmentManager = getSupportFragmentManager();
  26. }
  27. private void switchFragment(BaseFragment baseFragment) {
  28. FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
  29. fragmentTransaction.replace(R.id.test_page_container, baseFragment);
  30. fragmentTransaction.commit();
  31. }
  32. private void initListener() {
  33. mTestToast.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View view) {
  36. // 多次点击会造成toast错乱,显示不准确
  37. // Toast.makeText(TextNavigationBarActivity.this, "dsad", Toast.LENGTH_SHORT).show();
  38. // 使用Toast工具类
  39. ToastUtils.showToast("测试Toast");
  40. }
  41. });
  42. mNavigationBar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  43. @Override
  44. public void onCheckedChanged(RadioGroup radioGroup, int i) {
  45. switch (i) {
  46. case R.id.test_home:
  47. switchFragment(mHomePageFragment);
  48. break;
  49. case R.id.test_selected:
  50. switchFragment(mSelectedPageFragment);
  51. break;
  52. case R.id.test_red_packet:
  53. switchFragment(mRedPacketPageFragment);
  54. break;
  55. case R.id.test_search:
  56. switchFragment(mSearchPageFragment);
  57. break;
  58. }
  59. }
  60. });
  61. }
  62. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/219306
推荐阅读
相关标签
  

闽ICP备14008679号