繁体   English   中英

Android 底部导航栏 - 使用 ScrollView 布局

[英]Android bottom navigation bar - layout with ScrollView

我将如何配置我的底部导航栏以将其强制到屏幕底部?

示例底部导航栏:

    <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>

完整的 XML 在这里: https://snipit.io/public/snippets/66046

我尝试了各种东西,比如将东西放在视图和/或线性布局中。

对于底栏,我也尝试不使用 FrameLayout 而只使用线性/相对布局。

应用程序崩溃或底部栏位于屏幕中间的某个位置

在有 ScrollView 的情况下需要帮助弄清楚如何创建底部导航栏。

或者我正在做的事情不是很好的做法?

您不能将底部导航放在屏幕底部,因为您使用的是 ScrollView 作为根布局。 Scrollview 中的所有内容都将被滚动。 为了将底部导航保持在底部并固定,您应该将根布局更改为 ConstraintLayout 并将 ScrollView 放入其中,如下所示:

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

更推荐使用 NestedScrollView 而不是 Scrollview 因为它在动画方面更灵活

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM