【问题标题】:how can i show , add fab icon in centre using bottom navigation view我如何显示,使用底部导航视图在中心添加 fab 图标
【发布时间】:2021-05-08 19:20:32
【问题描述】:

我已经尝试过了,但它没有在中心显示更大的 fab 图标大小,并且在这个站点上使用了几乎解决方案。 我想实现如下 youtube 底部中心图标。

<!--  Bottom Menu -->
                    <RelativeLayout
                        android:id="@+id/home_page_navigation"
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="0.7"
                        android:background="@color/background_color"
                        android:gravity="bottom"
                        android:layout_marginTop="2dp"
                        android:orientation="horizontal">
    
    
                        <com.google.android.material.bottomnavigation.BottomNavigationView
                            android:id="@+id/navigation_view"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentBottom="true"
                            android:background="@color/bottom_navigation_view_color"
                            app:itemIconTint="@color/selector_item_primary_color"
                            app:itemTextColor="@color/selector_item_primary_color"
                            app:labelVisibilityMode="labeled"
                            app:menu="@menu/menu_navigation"/>
    
                        <com.google.android.material.floatingactionbutton.FloatingActionButton
                            android:id="@+id/fab_button"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentBottom="true"
                            android:layout_centerHorizontal="true"
                            android:src="@drawable/ic_add_icon"
                            android:tint="@color/colorGray"
                            android:background="@color/bottomcolor"
                            app:borderWidth="0dp"
                            app:elevation="9dp"
                            android:contentDescription="add" />
    
                    </RelativeLayout>

【问题讨论】:

    标签: android xml android-layout floating-action-button bottomnavigationview


    【解决方案1】:

    使用 ConstraintLayout 并应用

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
    

    参考https://riggaroo.dev/constraintlayout-guidelines-barriers-chains-groups/

    【讨论】:

      【解决方案2】:

      我已经使用 coordinator layout 实现了这一点,可能会对其他人有所帮助。这是我的代码

      <androidx.coordinatorlayout.widget.CoordinatorLayout
                              android:id="@+id/home_page_navigation"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"> 
                         <com.google.android.material.bottomnavigation.BottomNavigationView
                                  android:id="@+id/nav_view"
                                  android:layout_width="match_parent"
                                  android:layout_height="wrap_content"
                                  android:layout_gravity="bottom"
                                  android:background="@color/bottom_navigation_view_color"
                                  app:itemIconTint="@color/selector_item_primary_color"
                                  app:itemTextColor="@color/selector_item_primary_color"
                                  app:labelVisibilityMode="labeled"
                                  app:elevation="0dp"
                                  app:menu="@menu/menu_navigation"
                                  app:theme="@style/Theme.MaterialComponents"/>
      <com.google.android.material.floatingactionbutton.FloatingActionButton
                                  android:id="@+id/fab_button"
                                  android:layout_width="wrap_content"
                                  android:layout_height="wrap_content"
                                  android:layout_gravity="center"
                                  android:contentDescription="add"
                                  android:src="@drawable/ic_add_icon"
                                  app:backgroundTint="@color/bottom_navigation_view_color"
                                  app:elevation="0dp"
                                  app:layout_anchor="@+id/nav_view"
                                  app:layout_anchorGravity="center"
                                  app:maxImageSize="40dp" />
                          </androidx.coordinatorlayout.widget.CoordinatorLayout>`
      

      【讨论】:

      • 你能解释一下升技你是如何达到上图效果的吗?
      • 我使用了协调器布局并为大图标添加了单独的 fab。因此,它可以像 fab 一样自动化其子节点之间的协调,并有助于构建这种视图。
      【解决方案3】:

      对于那些正在寻找答案的人,我有一个完整的解决方案。

      1. 设置底部导航菜单
      <?xml version="1.0" encoding="utf-8"?>
      <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
      
          <item
              android:id="@+id/homeFragment"
              android:icon="@drawable/ic_home"
              android:title="@string/home"
              app:showAsAction="always" />
      
          <item
              android:id="@+id/shopFragment"
              android:icon="@drawable/ic_shop"
              app:showAsAction="always"
              android:title="@string/shop"/>
      
          // make the center icon have no title nor icon
          <item
              android:id="@+id/add"
              app:showAsAction="always"
              android:title=""/>
      
          <item
              android:id="@+id/notificationFragment"
              android:icon="@drawable/ic_notification"
              app:showAsAction="always"
              android:title="@string/notification"/>
      
          <item
              android:id="@+id/profileFragment"
              android:icon="@drawable/ic_profile"
              app:showAsAction="always"
              android:title="@string/profile"/>
      
      </menu>
      
      1. 使用ConstraintLayout 将中心按钮对齐到底部
      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity">
      
          <androidx.fragment.app.FragmentContainerView
              android:id="@+id/main_navhost_fragment"
              android:name="androidx.navigation.fragment.NavHostFragment"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              app:defaultNavHost="true"
              app:navGraph="@navigation/main_nav_graph"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintBottom_toTopOf="@id/fl_bottom_nav"/>
      
          <FrameLayout
              android:id="@+id/fl_bottom_nav"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintBottom_toBottomOf="parent">
      
              <com.google.android.material.bottomnavigation.BottomNavigationView
                  android:id="@+id/bottom_navigation"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="bottom"
                  app:elevation="4dp"
                  app:labelVisibilityMode="selected"
                  app:menu="@menu/menu_main_bottom_nav"
                  app:layout_constraintBottom_toBottomOf="parent"/>
      
          </FrameLayout>
      
          <ImageView
              android:id="@+id/iv_add_media"
              android:layout_width="40dp"
              android:layout_height="40dp"
              android:src="@drawable/ic_baseline_add_circle_outline_24"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="@id/fl_bottom_nav"/>
      
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      结果是这样的。

      1. 在activity中调用iv_add_media的onclickListener
      binding.ivAddMedia.setOnClickListener {
          // you can navigate to any fragment in the nav_graph
          // for my case I will open another activity
          navController.navigate(R.id.mediaActivity)
      }
      

      如果有更好的解决方案,请告诉我。谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多