【问题标题】:Android UI - dynamic button alignment changeAndroid UI - 动态按钮对齐更改
【发布时间】:2021-10-08 02:29:50
【问题描述】:

我有以下 xml

<ConstraintLayout>
    <NestedScrollView>
        <ConstraintLayout>
            <RecycleView/>
        </ConstraintLayout>
    </NestedScrollView>
    <Button BottomTo_BottomOf = parent/>
</ConstraintLayout>

当我加载屏幕时,我希望按钮贴在屏幕底部。 我在 recycleview 中动态加载一些元素(基于用户操作),一旦 recycleview 的元素超过屏幕大小,我希望按钮对齐方式从屏幕底部更改到内容的末尾,并且在用户滚动浏览时可见全部内容

【问题讨论】:

  • 你的布局有多少是为了适应按钮的位置,有多少是为了支持你设计的其他方面?例如,您有一个 RecyclerView 嵌套在一个 ConstraintLayout 中,该 ConstraintLayout 嵌套在一个看起来多余的 NestedScrollView 中,并且所有内容都由外部 约束布局。如果按钮可以在 NestedScrollView 内移动,您还需要外部 ConstraintLayout 吗?也许您发布的所有内容都是必需的。如果没有,它可能有助于显示您的设计所需的最小骨架布局没有按钮。

标签: android android-layout user-interface android-fragments android-constraintlayout


【解决方案1】:

你需要在按钮上添加下面的标签 -

 app:layout_constraintBottom_toBottomOf="parent"

【讨论】:

  • 最初 m 使用它,但是当我在 recycleview 中有更多内容时,我必须在最后按下按钮,而不是将其保留在屏幕底部和属性上,你建议我已经在我的问题
  • 如果你想在内容的底部添加它,那么你需要在recyclerview下面添加它。
【解决方案2】:

将这些属性添加到Button:

    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"

将这些属性添加到NestedScrollView

    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/bottomButtonId" <--Id of bottom button-->

作为参考,试试这个代码:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button6">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvMain"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:overScrollMode="never"
                tools:listitem="@layout/imagepost_buttons"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bottom Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 2012-12-06
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2017-11-28
    相关资源
    最近更新 更多