Android底部导航栏 - 带有ScrollView的布局
我如何配置底部的Navbar将其强加于屏幕的底部?
示例底部导航栏:
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_menu" />
我的布局(activity_main.xml)看起来像:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<ImageView
android:id="@+id/childPic"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="40dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/baby"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
More Image and TextViews
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
full xml:
对于底栏,我还尝试不使用Framelayout和线性/相对布局。
应用程序崩溃或底栏位于我的屏幕中间的某个地方
需要帮助,鉴于有一个滚动浏览器,可以弄清楚如何创建底部导航栏。
还是我做的事情不好?
How would I configure my bottom navbar to force it to the bottom of the screen?
Example bottom nav bar:
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="56dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_menu" />
My layout (activity_main.xml) looks like:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<ImageView
android:id="@+id/childPic"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="40dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/baby"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
More Image and TextViews
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Full XML here: https://snipit.io/public/snippets/66046
I tried various things like putting things in Views and/or Linear Layouts.
For the bottom bar, I also tried not using FrameLayout and just Linear/Relative Layout.
Either the app crashes or the bottom bar is somewhere in the middle of my screen
Need assistance figuring out how to create a bottom nav bar given that there is a ScrollView.
Or is something I am doing not good practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法将底部导航放在屏幕的底部,因为您正在使用
ScrollView作为根布局。 ScrollView中的所有内容都将滚动。为了将底部导航保持在底部和
修复
动画更灵活
You cannot put your bottom navigation in the bottom of screen, because you are using
ScrollView as a root layout. Everything inside Scrollview will be Scrolled. In order to keep bottom navigation in the bottom and fixed you should change your root layout to ConstraintLayout and put ScrollView inside it like:
It is more recommended to use NestedScrollView instead of Scrollview as
it is more flexible with animations