Android底部导航栏 - 带有ScrollView的布局

发布于 2025-01-25 08:10:22 字数 2349 浏览 5 评论 0原文

我如何配置底部的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2025-02-01 08:10:22

您无法将底部导航放在屏幕的底部,因为您正在使用
ScrollView作为根布局。 ScrollView中的所有内容都将滚动。为了将底部导航保持在底部和

<ConstraintLayout 
layout_height="match_parent>
<ScrollView
layout_height="wrap_content">
... Scrollable views here
</ScrollView>
<BottomNavigationView/>
</ConstraintLayout>

修复
动画更灵活

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:

<ConstraintLayout 
layout_height="match_parent>
<ScrollView
layout_height="wrap_content">
... Scrollable views here
</ScrollView>
<BottomNavigationView/>
</ConstraintLayout>

It is more recommended to use NestedScrollView instead of Scrollview as
it is more flexible with animations

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文