Sum*_*kla 5 android android-layout floating-action-button android-coordinatorlayout bottomnavigationview
我目前正在和BottomNavigationView和一起工作FloatingActionButton。我要实现的是以下设计:
而我尝试了:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
app:layout_insetEdge="bottom"
tools:context=".activity.BottomNavPrimary">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_insetEdge="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_primary"></android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
sun*_*rer 10
你的设计看起来好像要使用新的BottomAppBar从MaterialComponents与Android P.将完全是发行特别是如果左边的图标代表了一种侧面导航的它可能是正确的导航元素。
但是,您必须注意,FAB 左侧和右侧的元素与底部导航具有不同的用途。它不是应用程序中“主要目的地”的入口点,而是被BottomAppBar定义为:
底部应用栏在移动屏幕底部显示导航和关键操作。
所以它用于页面操作(如打开仪表板或搜索)。更多解释可以在设计文档中找到。
我还没有机会实现它(因为我真的需要底部导航),但这里是代码文档示例:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Other components and views -->
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:navigationIcon="@drawable/ic_menu_24"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bar"/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
所以对我来说,听起来好像你定义了你的两个页面菜单选项,因为 FAB 固定在栏上,它会将它们推到两侧。
该文档还包括今年 Google I/O 期间展示的可选 FAB 支架的选项,并展示了如何处理菜单和点击处理。
这是关于如何设置 gradle以在项目中包含新材料组件的另一个有用链接。
小智 10
我用这个,得到类似的东西。
BottomNavigationView + FloatingActionButton
<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating_button"
style="@style/Widget.MaterialComponents.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="16dp"
app:backgroundTint="@color/colorPrimaryLight"
app:elevation="@dimen/padding_10"
app:layout_constraintBottom_toTopOf="@id/navigation"
app:layout_constraintLeft_toRightOf="@id/navigation"
app:layout_constraintRight_toLeftOf="@id/navigation"
app:layout_constraintTop_toBottomOf="@id/navigation"
app:layout_constraintTop_toTopOf="@id/navigation"
app:layout_insetEdge="bottom"
app:srcCompat="@drawable/ic_add_black_24dp"
app:tint="@color/colorPrimary" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimaryDark"
android:visibility="visible"
app:itemIconTint="@drawable/bottom_navigation_icons"
app:itemTextColor="@drawable/bottom_navigation_icons"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
我的解决方案简单而酷。
父布局必须是 aCoordinatorLayout并使用BottomAppBarwith FloatingActionButton,如下所示
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<-- your other views -->
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="@color/colorPrimary">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/homePage"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.4">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/your_drawable"
android:padding="@dimen/small_margin"
app:tint="@color/white" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/profile"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.4">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:padding="@dimen/small_margin"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/your_drawable" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/categories"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_drawable"
app:backgroundTint="@color/colorPrimary"
app:layout_anchor="@id/navigation"
app:tint="@android:color/white" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
结果: