• BottomNavigation 底部导航模版


    1. build.gradle 添加引用库

    1. implementation 'androidx.navigation:navigation-fragment:2.5.1'
    2. implementation 'androidx.navigation:navigation-ui:2.5.1'

    2. 创建资源文件

      2.1 ic_baseline_looks_one_24.xml

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="24dp"
    3. android:height="24dp"
    4. android:tint="#000000"
    5. android:viewportWidth="24"
    6. android:viewportHeight="24">
    7. <path
    8. android:fillColor="@android:color/white"
    9. android:pathData="M19,3L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM14,17h-2L12,9h-2L10,7h4v10z" />
    10. vector>

      2.2 ic_baseline_looks_two_24.xml

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="24dp"
    3. android:height="24dp"
    4. android:tint="#000000"
    5. android:viewportWidth="24"
    6. android:viewportHeight="24">
    7. <path
    8. android:fillColor="@android:color/white"
    9. android:pathData="M19,3L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM15,11c0,1.11 -0.9,2 -2,2h-2v2h4v2L9,17v-4c0,-1.11 0.9,-2 2,-2h2L13,9L9,9L9,7h4c1.1,0 2,0.89 2,2v2z" />
    10. vector>

      2.3 ic_baseline_looks_three_24.xml

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="24dp"
    3. android:height="24dp"
    4. android:tint="#000000"
    5. android:viewportWidth="24"
    6. android:viewportHeight="24">
    7. <path
    8. android:fillColor="@android:color/white"
    9. android:pathData="M19.01,3h-14c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21.01,5c0,-1.1 -0.9,-2 -2,-2zM15.01,10.5c0,0.83 -0.67,1.5 -1.5,1.5 0.83,0 1.5,0.67 1.5,1.5L15.01,15c0,1.11 -0.9,2 -2,2h-4v-2h4v-2h-2v-2h2L13.01,9h-4L9.01,7h4c1.1,0 2,0.89 2,2v1.5z" />
    10. vector>

      2.4 表情图标 ic_baseline_insert_emoticon_24.xml

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="24dp"
    3. android:height="24dp"
    4. android:tint="#0E856F"
    5. android:viewportWidth="24"
    6. android:viewportHeight="24">
    7. <path
    8. android:fillColor="@android:color/white"
    9. android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8zM15.5,11c0.83,0 1.5,-0.67 1.5,-1.5S16.33,8 15.5,8 14,8.67 14,9.5s0.67,1.5 1.5,1.5zM8.5,11c0.83,0 1.5,-0.67 1.5,-1.5S9.33,8 8.5,8 7,8.67 7,9.5 7.67,11 8.5,11zM12,17.5c2.33,0 4.31,-1.46 5.11,-3.5L6.89,14c0.8,2.04 2.78,3.5 5.11,3.5z" />
    10. vector>

    3. 创建菜单文件 menu 目录下 menu.xml

    1. "1.0" encoding="utf-8"?>
    2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item
    4. android:id="@+id/first_fragment"
    5. android:icon="@drawable/ic_baseline_looks_one_24"
    6. android:title="旋转" />
    7. <item
    8. android:id="@+id/second_fragment"
    9. android:icon="@drawable/ic_baseline_looks_two_24"
    10. android:title="缩放" />
    11. <item
    12. android:id="@+id/third_fragment"
    13. android:icon="@drawable/ic_baseline_looks_three_24"
    14. android:title="移动" />
    15. menu>

    4. 创建First页 Fragment,ViewModel 及布局文件

      4.1 FirstFragment.java

    1. public class FirstFragment extends Fragment {
    2. private FirstViewModel mViewModel;
    3. private ImageView imageView;
    4. public static FirstFragment newInstance() {
    5. return new FirstFragment();
    6. }
    7. @Override
    8. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
    9. @Nullable Bundle savedInstanceState) {
    10. View view = inflater.inflate(R.layout.fragment_first, container, false);
    11. imageView = view.findViewById(R.id.imageView);
    12. return view;
    13. }
    14. @Override
    15. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    16. super.onViewCreated(view, savedInstanceState);
    17. mViewModel = new ViewModelProvider(this).get(FirstViewModel.class);
    18. imageView.setRotation(mViewModel.rotationPosition);
    19. ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0, 0);
    20. objectAnimator.setDuration(500);
    21. imageView.setOnClickListener(new View.OnClickListener() {
    22. @Override
    23. public void onClick(View view) {
    24. if (!objectAnimator.isRunning()) {
    25. objectAnimator.setFloatValues(imageView.getRotation(), imageView.getRotation() + 100);
    26. mViewModel.rotationPosition += 100;
    27. objectAnimator.start();
    28. }
    29. }
    30. });
    31. }
    32. }

      4.2 FirstViewModel.java

    1. public class FirstViewModel extends ViewModel {
    2. float rotationPosition = 0;
    3. }

      4.3 fragment_first.xml

    1. "1.0" encoding="utf-8"?>
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".FirstFragment">
    8. <ImageView
    9. android:id="@+id/imageView"
    10. android:layout_width="100dp"
    11. android:layout_height="100dp"
    12. android:layout_gravity="center"
    13. android:src="@drawable/ic_baseline_insert_emoticon_24" />
    14. FrameLayout>

    5. 创建Second页 Fragment,ViewModel 及布局文件

      5.1 SecondFragment.java

    1. public class SecondFragment extends Fragment {
    2. private SecondViewModel mViewModel;
    3. private ImageView imageView;
    4. @Override
    5. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    6. return inflater.inflate(R.layout.fragment_second, container, false);
    7. }
    8. @Override
    9. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    10. super.onViewCreated(view, savedInstanceState);
    11. mViewModel = new ViewModelProvider(this).get(SecondViewModel.class);
    12. imageView = view.findViewById(R.id.imageView);
    13. imageView.setScaleX(mViewModel.scaleFactor);
    14. imageView.setScaleY(mViewModel.scaleFactor);
    15. ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(imageView, "scaleX", 0, 0);
    16. ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(imageView, "scaleY", 0, 0);
    17. objectAnimatorX.setDuration(500);
    18. objectAnimatorY.setDuration(500);
    19. imageView.setOnClickListener(new View.OnClickListener() {
    20. @Override
    21. public void onClick(View view) {
    22. if (!objectAnimatorX.isRunning()) {
    23. objectAnimatorX.setFloatValues(imageView.getScaleX() + 1.0f);
    24. objectAnimatorY.setFloatValues(imageView.getScaleY() + 1.0f);
    25. mViewModel.scaleFactor += 1.0f;
    26. objectAnimatorX.start();
    27. objectAnimatorY.start();
    28. }
    29. }
    30. });
    31. }
    32. }

      5.2 SecondViewModel.java

    1. public class SecondViewModel extends ViewModel {
    2. float scaleFactor = 1;
    3. }

      5.3 fragment_second.xml

    1. "1.0" encoding="utf-8"?>
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".SecondFragment">
    8. <ImageView
    9. android:id="@+id/imageView"
    10. android:layout_width="100dp"
    11. android:layout_height="100dp"
    12. android:layout_gravity="center"
    13. android:src="@drawable/ic_baseline_insert_emoticon_24" />
    14. FrameLayout>

    6. 创建 Third 页 Fragment,ViewModel,xml文件

      6.1 ThirdFragment.java

    1. public class ThirdFragment extends Fragment {
    2. private ThirdViewModel mViewModel;
    3. private ImageView imageView;
    4. @Override
    5. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    6. View view = inflater.inflate(R.layout.fragment_third, container, false);
    7. imageView = view.findViewById(R.id.imageView);
    8. return view;
    9. }
    10. @Override
    11. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    12. super.onViewCreated(view, savedInstanceState);
    13. mViewModel = new ViewModelProvider(this).get(ThirdViewModel.class);
    14. imageView.setX(imageView.getX() + mViewModel.dx);
    15. ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "x", 0, 0);
    16. objectAnimator.setDuration(500);
    17. imageView.setOnClickListener(new View.OnClickListener() {
    18. @Override
    19. public void onClick(View view) {
    20. if (!objectAnimator.isRunning()) {
    21. float dx = new Random().nextBoolean() ? 100 : -100;
    22. objectAnimator.setFloatValues(imageView.getX(), imageView.getX() + dx);
    23. mViewModel.dx += dx;
    24. objectAnimator.start();
    25. }
    26. }
    27. });
    28. }
    29. }

      6.2 ThirdViewModel.java

    1. public class ThirdViewModel extends ViewModel {
    2. float dx;
    3. }

      6.3 fragment_third.xml

    1. "1.0" encoding="utf-8"?>
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".ThirdFragment">
    8. <ImageView
    9. android:id="@+id/imageView"
    10. android:layout_width="100dp"
    11. android:layout_height="100dp"
    12. android:layout_gravity="center"
    13. android:src="@drawable/ic_baseline_insert_emoticon_24" />
    14. FrameLayout>

    7. 创建 navigation.xml 文件, 在 navigation 目录下

    1. "1.0" encoding="utf-8"?>
    2. <navigation xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:id="@+id/navigation"
    6. app:startDestination="@id/first_fragment">
    7. <fragment
    8. android:id="@+id/first_fragment"
    9. android:name="com.example.bottomnavigationdemo.FirstFragment"
    10. android:label="旋转"
    11. tools:layout="@layout/fragment_first" />
    12. <fragment
    13. android:id="@+id/second_fragment"
    14. android:name="com.example.bottomnavigationdemo.SecondFragment"
    15. android:label="缩放"
    16. tools:layout="@layout/fragment_second" />
    17. <fragment
    18. android:id="@+id/third_fragment"
    19. android:name="com.example.bottomnavigationdemo.ThirdFragment"
    20. android:label="移动"
    21. tools:layout="@layout/fragment_third" />
    22. navigation>

    8. 测试文件 Main 页

      8.1 MainActivity.java

    1. public class MainActivity extends AppCompatActivity {
    2. @Override
    3. protected void onCreate(Bundle savedInstanceState) {
    4. super.onCreate(savedInstanceState);
    5. setContentView(R.layout.activity_main);
    6. BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
    7. NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentContainerView);
    8. NavController navController = navHostFragment.getNavController();
    9. // navController.getGraph() //R.id.first_fragment,R.id.second_fragment,R.id.third_fragment
    10. AppBarConfiguration configuration = new AppBarConfiguration.Builder(bottomNavigationView.getMenu()).build();
    11. NavigationUI.setupActionBarWithNavController(this, navController, configuration);
    12. NavigationUI.setupWithNavController(bottomNavigationView,navController);
    13. }
    14. }

      8.2 activity_main.xml

    1. "1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".MainActivity">
    8. <com.google.android.material.bottomnavigation.BottomNavigationView
    9. android:id="@+id/bottomNavigationView"
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. app:layout_constraintBottom_toBottomOf="parent"
    13. app:layout_constraintEnd_toEndOf="parent"
    14. app:layout_constraintStart_toStartOf="parent"
    15. app:menu="@menu/menu" />
    16. <androidx.fragment.app.FragmentContainerView
    17. android:id="@+id/fragmentContainerView"
    18. android:name="androidx.navigation.fragment.NavHostFragment"
    19. android:layout_width="match_parent"
    20. android:layout_height="0dp"
    21. app:defaultNavHost="true"
    22. app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
    23. app:layout_constraintEnd_toEndOf="parent"
    24. app:layout_constraintStart_toStartOf="parent"
    25. app:layout_constraintTop_toTopOf="parent"
    26. app:navGraph="@navigation/navigation" />
    27. androidx.constraintlayout.widget.ConstraintLayout>

    9. 效果图

        

  • 相关阅读:
    深入浅出排序算法之希尔排序
    JDK8和JDK17共存以及切换
    数据分析 — Matplotlib 、Pandas、Seaborn 绘图
    flutter Failed to download https://flutter-io.cn/flutter_infra_release/
    满级大牛 HuaWei 首次出这份 598 页【网络协议全彩手册】,建议大家收藏
    解析QAnything启动命令过程
    基于JAVA手办周边商城计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    FutureTask配合Thread实现处理有返回结果的源码、逻辑与架构分析
    使用Plotly模拟远古博弈游戏_掷骰子
    30天拿下Rust之前世今生
  • 原文地址:https://blog.csdn.net/u011193452/article/details/126834009