我如何显示,使用底部导航视图在中心添加 fab 图标
how can i show , add fab icon in centre using bottom navigation view
我已经试过了,但它没有在中心显示更大的 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>
使用ConstraintLayout并应用
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
参考https://riggaroo.dev/constraintlayout-guidelines-barriers-chains-groups/
我已经使用协调器布局实现了这一点,可能会对其他人有所帮助。这是我的代码
<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>`
对于那些正在寻找答案的人,我有一个完整的解决方案。
- 设置bottom_nav菜单
<?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>
- 使用
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>
结果是这样的。
- 在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)
}
如果有更好的解决办法请告诉我。谢谢
我已经试过了,但它没有在中心显示更大的 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>
使用ConstraintLayout并应用
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
参考https://riggaroo.dev/constraintlayout-guidelines-barriers-chains-groups/
我已经使用协调器布局实现了这一点,可能会对其他人有所帮助。这是我的代码
<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>`
对于那些正在寻找答案的人,我有一个完整的解决方案。
- 设置bottom_nav菜单
<?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>
- 使用
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>
结果是这样的。
- 在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)
}
如果有更好的解决办法请告诉我。谢谢