android - 如何实现带有底部导航的导航组件?
问题描述
我有一个活动和三个片段(AddFragment、QuoteFragment 和 CalendarFragment)。主要活动在底部应用栏和片段容器内有一个底部导航。我想要的是将底部导航项与片段链接,有什么简单的方法吗?
activity_main.xml
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"/>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="@menu/bottom_app_bar"
android:background="@android:color/transparent"
/>
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add_24"
app:layout_anchor="@id/bottomAppBar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.bottomNavigation.setBackground(null)
}
}
解决方案
推荐阅读
- facebook-graph-api - 获取Access Token Server端Instagram Graph Api V3
- ruby - 当变量可能为假时,如何检查 nil 并在同一行初始化?
- docusignapi - Docusign - 使用 REST API 填充信封中的选项卡
- javascript - Javascript array.push 替换以前添加的元素
- rest - 使用 Dynamics 365 Web api 创建记录时跳过字段验证
- java - Volley 似乎不适用于 Android SDK 29
- jquery - jquery-selective隐藏条件上的删除图标
- apache-kafka - Couchbase kafka 源连接器消息顺序
- python - 修复将二进制数据写入文件的问题:“文件不是 UTF-8 编码的”
- php - PHP:以浮点数计算完整年龄