LinearLayout 和 NestedScrollView 问题

问题描述

我想如何使用 nestedScrollView,顶部的工具栏视图和底部底部视图视图。顶视图和底视图工具栏和底视图必须始终就位。

如何为任何设备实现此结果?感谢您的回答,谢谢。

总是这样:

enter image description here

这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">
    <!--This is top-->
    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/colorPrimary">
        <!--.........................-->
    </androidx.appcompat.widget.LinearLayoutCompat>


    <androidx.core.widget.nestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/colorGreyFragment">
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </androidx.constraintlayout.widget.ConstraintLayout>
        </LinearLayout>
    </androidx.core.widget.nestedScrollView>

    <!--This is bottom-->
    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/bottom_buttons_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:background="@color/colorWhite">
        <!--..............................-->
    </androidx.appcompat.widget.LinearLayoutCompat>

</LinearLayout>

解决方法

你可以这样做,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">
    <!--This is top-->
    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/colorPrimary">
        <!--.........................-->
    </androidx.appcompat.widget.LinearLayoutCompat>


    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorGreyFragment"
            android:orientation="vertical">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></androidx.constraintlayout.widget.ConstraintLayout>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

    <!--This is bottom-->
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

我已经用 BottomNavigationView 替换了底部的线性布局,如果你想要其他东西,你可以在那里替换。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...