![](/img/trans.png)
[英]Android Scroll View last view not showing inside linear layout
[英]Android Constraint Layout in Scroll View not showing the last item
我正在尝试制作一个具有大量 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/costumer_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/descriptionInput" />
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="@+id/costumerSpinner"
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/costumerSpinner" />
<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
你必须改变
app:layout_constraintTop_toBottomOf="parent"
至
app:layout_constraintBottom_toBottomOf="parent"
基本上你说'按钮顶部'应该从父级下方开始。
您是否尝试过制作虚拟物品? 换句话说,添加一些完全透明(不可见)但会在按钮后占用物理空间的东西? 比如一个普通的 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
(删除加号)
只是为了确保元素存在,并且没有被片段的限制隐藏,尝试在你的 constraintLayout 上添加一个底部边距,如下所示:
android:layout_marginBottom="40dp"
使用滚动视图,您应该能够导航到最后一项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.