【问题标题】:Android Constraint Layout in Scroll View not showing the last item滚动视图中的Android约束布局不显示最后一项
【发布时间】:2021-02-09 12:59:58
【问题描述】:

我正在尝试制作一个具有大量 textView、EditText 和微调器的表单的片段,因此我必须使用 scrollView 来查看所有项目。

由于某种原因,最后一项没有显示,我不明白为什么,我阅读了很多关于这个问题的问题,但没有人适合我。

谁能帮我解决这个问题?

xml:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        tools:context=".UI.newTicket.NewTicketFragment">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/formLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="60dp">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/titleText"
                style="@style/TitleText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:text="@string/new_ticket_text"
                android:textColor="@color/white"
                app:layout_constraintBottom_toBottomOf="@id/toolbar"
                app:layout_constraintStart_toStartOf="@id/toolbar"
                app:layout_constraintTop_toTopOf="@id/toolbar" />

            <TextView
                android:id="@+id/textView4"
                style="@style/TitleText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="20dp"
                android:text="@string/title_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/toolbar" />

            <EditText
                android:id="@+id/titleInput"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                android:autofillHints="Titolo del ticket"
                android:background="@drawable/edit_text_border"
                android:ems="10"
                android:hint="@string/insertTitle_text"
                android:inputType="text"
                android:maxLines="1"
                android:padding="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/textView4" />

            <TextView
                android:id="@+id/textView5"
                style="@style/TitleStyle"
                android:text="@string/description_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/titleInput" />

            <EditText
                android:id="@+id/descriptionInput"
                android:layout_width="350dp"
                android:layout_height="70dp"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                android:autofillHints="Descrizione del ticket"
                android:background="@drawable/edit_text_border"
                android:ems="10"
                android:hint="@string/InsertDescr_text"
                android:inputType="textMultiLine"
                android:padding="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/textView5" />

            <TextView
                android:id="@+id/TextView6"
                style="@style/TitleStyle"
                android:text="@string/customer_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/descriptionInput" />

            <com.toptoche.searchablespinnerlibrary.SearchableSpinner
                android:id="@+id/customerSpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView6" />

            <TextView
                android:id="@+id/TextView7"
                style="@style/TitleStyle"
                android:text="@string/field_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/customerSpinner" />

            <Spinner
                android:id="@+id/fieldSpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView7" />

            <TextView
                android:id="@+id/TextView8"
                style="@style/TitleStyle"
                android:text="@string/category_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/fieldSpinner" />

            <Spinner
                android:id="@+id/categorySpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView8" />


            <TextView
                android:id="@+id/TextView9"
                style="@style/TitleStyle"
                android:text="@string/priority_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/categorySpinner" />

            <Spinner
                android:id="@+id/prioritySpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView9" />

            <TextView
                android:id="@+id/deadlineTitle"
                style="@style/TitleStyle"
                android:text="@string/deadline_text"
                android:visibility="visible"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/prioritySpinner" />

            <EditText
                android:id="@+id/deadlineInput"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                android:autofillHints="Scadenza del ticket"
                android:background="@drawable/edit_text_border"
                android:ems="10"
                android:hint="@string/deadlineHint_text"
                android:inputType="date"
                android:visibility="visible"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/deadlineTitle" />


            <TextView
                android:id="@+id/TextView11"
                style="@style/TitleStyle"
                android:text="@string/AssignTo_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/deadlineInput" />

            <Spinner
                android:id="@+id/assignmentSpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView11" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/createButton"
                style="@style/createButton"
                android:layout_width="350dp"
                android:layout_height="60dp"
                android:layout_marginTop="10dp"
                android:text="@string/create_text"
                app:cornerRadius="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/assignmentSpinner"
                app:strokeColor="@color/light_blue"
                app:strokeWidth="2dp" />


            <!--  Used only for show the button
            <TextView
                android:id="@+id/uselessItem"
                android:layout_width="1dp"
                android:layout_height="50dp"
                android:visibility="invisible"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/createButton"
                />-->

        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</layout>

当我使用应用程序时,最后一个项目(按钮)没有显示,我滚动并查看除按钮之外的所有项目。

更新 在重量为 60dp 的按钮之后使用无用的项目允许我滚动并查看按钮,我不喜欢它但似乎是唯一的解决方案

更新 2 我放了完整的xml

【问题讨论】:

  • 请有人帮我,我被卡住了
  • 你的 assignmentSpinner 在哪里?
  • 从 com.google.android.material.button.MaterialButton 中删除此应用:layout_constraintTop_toBottomOf="@+id/assignmentSpinner",看看发生了什么
  • @RajendraMahato 它是按钮上方的微调器,我没有放入 xml 示例。如果我删除应用程序:layout_constraintTop_toBottomOf="@+id/assignmentSpinner 按钮会在片段顶部上升

标签: java android xml kotlin


【解决方案1】:

您是否尝试过制作虚拟物品? 换句话说,添加一些完全透明(不可见)但会在按钮之后占用物理空间的东西?比如一个普通的TextView,带有这样的参数:

 <TextView
                android:layout_width="1dp"
                android:layout_height="60dp"
                android:text="-------"
                android:textColor="@color/transparent" <!-- use color code #ff00000 -->
                android:textSize="30sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/yourlastbuttonIDhere"/>

此外,对于最佳实践,需要注意一件事:设置约束时,例如此处

                app:layout_constraintTop_toBottomOf="@+id/assignmentSpinner"

您不使用@+id/assignmentSpinner,而是使用@id/assignmentSpinner (去掉加号)

【讨论】:

  • 是的,它可以使用虚拟项目,但我必须将高度设置为 60dp 才能显示按钮,由于某种原因,最后一个项目被切断了。我宁愿在没有无用的项目的情况下解决这个问题
  • 您是否尝试过约束按钮的底部?我怀疑这是否可行,但值得一试:app:layout_constraintBottom_toBottomOf="parent" 您也可以在此处发布整个 XML 代码,以便我可以将其复制到我的 IDE 并尝试解决问题并提供反馈
  • 如果你想试试,我把所有的xml都放了
【解决方案2】:

你必须改变

app:layout_constraintTop_toBottomOf="parent"

app:layout_constraintBottom_toBottomOf="parent"

基本上你说“按钮顶部”应该从父级下方开始。

【讨论】:

  • 如果我进行此更改,按钮将与必须在其上方的微调器重叠
  • @zuzizuza675 对不起,我犯了一个复制错误......现在当你将它限制在父母身上时它应该可以工作
  • 现在按钮上升到片段的顶部,编译器说:ID“父”未在任何地方定义,因为我没有具有此ID的项目。如果我更改为 app:layout_constraintBottom_toBottomOf="parent" 按钮会上升到片段的顶部,我希望它在微调器@Thomas Winter 之后
  • 我也尝试从约束改为线性布局,但没有任何改变
  • 如果它在微调器前面你也可以尝试添加 app:layout_constraintTop_toBottomOf="@id/assignmentSpinner" 到按钮
【解决方案3】:

只是为了确保元素存在,并且没有被片段的限制隐藏,尝试在你的 constraintLayout 上添加一个底部边距,如下所示:

        android:layout_marginBottom="40dp"

使用滚动视图,您应该能够导航到最后一项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2020-07-25
    相关资源
    最近更新 更多