滚动视图顶部的静态按钮 android studio

Posted

技术标签:

【中文标题】滚动视图顶部的静态按钮 android studio【英文标题】:Static buttons over top a scroll view android studio 【发布时间】:2020-09-07 15:06:40 【问题描述】:

我想创建一个活动,其中我在屏幕底部有 2 个按钮,在它们上方有一个滚动视图。在滚动视图内部,我想要一个下拉菜单,用户可以在其中选择一系列练习。如何将滚动视图放在按钮上方并让它们在滚动时保持在同一位置?

活动代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:id="@+id/ConstraintLayout"
android:layout_
android:layout_
tools:context=".WorkoutsCreater">


<TextView
    android:id="@+id/NameWorkoutTextView"
    android:layout_
    android:layout_
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:gravity="center"
    android:text="Name of Workout:"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textSize="15dp"
    app:layout_constraintBottom_toBottomOf="@+id/NameWorkoutInput"
    app:layout_constraintEnd_toStartOf="@+id/NameWorkoutInput"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/NameWorkoutInput" />

<TextView
    android:id="@+id/nameExercise1"
    android:layout_
    android:layout_
    android:layout_marginStart="20dp"
    android:gravity="center"
    android:text="Exercise 1:"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textSize="15dp"
    app:layout_constraintBottom_toBottomOf="@id/spinnerExercise1"
    app:layout_constraintEnd_toStartOf="@+id/NameWorkoutInput"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/spinnerExercise1" />

<EditText

    android:id="@+id/NameWorkoutInput"
    android:layout_
    android:layout_
    android:layout_marginTop="20dp"
    android:layout_marginEnd="40dp"
    android:layout_marginRight="40dp"
    android:inputType="text"
    android:maxLength="20"
    android:textSize="15dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/NameWorkoutTextView"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/BtnNew"
    android:layout_
    android:layout_
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    android:background="@drawable/circle_plus"
    android:clickable="true"
    android:focusable="true"
    android:foreground="@drawable/ic_add_white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@drawable/ic_add_white" />

<Button
    android:id="@+id/BtnSave"
    android:layout_
    android:layout_
    android:layout_marginStart="16dp"
    android:layout_marginBottom="16dp"
    android:background="@drawable/rounded_corners"
    android:foreground="@drawable/ic_save_white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<Spinner
    android:id="@+id/spinnerExercise1"
    android:layout_
    android:layout_
    android:layout_marginTop="50dp"
    android:layout_marginEnd="40dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/NameWorkoutInput" />

活动图片

【问题讨论】:

【参考方案1】:

如果你想添加一个停留在两个按钮上方的滚动视图,你可以在约束布局内添加一个 NestedScrollview

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:id="@+id/ConstraintLayout"
    android:layout_
    android:layout_
    tools:context=".WorkoutsCreater">

    <androidx.core.widget.NestedScrollView
        android:layout_
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/BtnNew"
        android:layout_
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_
            android:layout_>

            <TextView
                android:id="@+id/NameWorkoutTextView"
                android:layout_
                android:layout_
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:gravity="center"
                android:text="Name of Workout:"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                app:layout_constraintBottom_toBottomOf="@+id/NameWorkoutInput"
                app:layout_constraintEnd_toStartOf="@+id/NameWorkoutInput"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/NameWorkoutInput" />

            <TextView
                android:id="@+id/nameExercise1"
                android:layout_
                android:layout_
                android:layout_marginStart="20dp"
                android:gravity="center"
                android:text="Exercise 1:"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                app:layout_constraintBottom_toBottomOf="@id/spinnerExercise1"
                app:layout_constraintEnd_toStartOf="@+id/NameWorkoutInput"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/spinnerExercise1" />

            <EditText
                android:id="@+id/NameWorkoutInput"
                android:layout_
                android:layout_
                android:layout_marginTop="20dp"
                android:layout_marginEnd="40dp"
                android:layout_marginRight="40dp"
                android:inputType="text"
                android:maxLength="20"
                android:textSize="15dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/NameWorkoutTextView"
                app:layout_constraintTop_toTopOf="parent" />

            <Spinner
                android:id="@+id/spinnerExercise1"
                android:layout_
                android:layout_
                android:layout_marginTop="50dp"
                android:layout_marginEnd="40dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/NameWorkoutInput" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>


    <Button
        android:id="@+id/BtnNew"
        android:layout_
        android:layout_
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/ic_add_circle"
        android:clickable="true"
        android:focusable="true"
        android:foreground="@drawable/ic_add_white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_add_white" />

    <Button
        android:id="@+id/BtnSave"
        android:layout_
        android:layout_
        android:layout_marginStart="16dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/ic_remove_red_ba0a00"
        android:foreground="@drawable/ic_save_white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

免责声明:嵌套布局比平面布局更昂贵

请看看这个https://developer.android.com/topic/performance/rendering/optimizing-view-hierarchies

【讨论】:

以上是关于滚动视图顶部的静态按钮 android studio的主要内容,如果未能解决你的问题,请参考以下文章

图像顶部的Android按钮不起作用

如何将按钮放在回收站视图的顶部?

在滚动视图上的按钮顶部添加图像

滚动位置在列表视图的顶部

Android:隐藏屏幕顶部边缘的视图

主视图从滚动视图中占据顶部空间?