Android scrollview消散了我的UI的其余部分
我是Android布局设计的新手。我的问题是,当尝试添加 scrollview 我的布局消失了我的剩下的UI。在以下代码中,当我删除卷轴时,它会发生上述问题。
<?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"
tools:context=".activity.SettingActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:title="Toolbar Title" />
</LinearLayout>
<!-- <ScrollView-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent">-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="1dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 1"
android:textAlignment="center"
android:textSize="15sp"
android:textStyle="bold" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="193dp"
android:layout_height="48dp"
android:text="Radio 1" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="Radio 2" />
</RadioGroup>
<View style="@style/Divider" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 2"
android:textAlignment="center"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 2 Description"
android:textAlignment="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sub-option 2.1"
android:textStyle="bold" />
<EditText
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:autofillHints="no"
android:ems="10"
android:inputType="numberSigned"
android:minHeight="48dp"
android:text="2"
android:textAlignment="center"
tools:ignore="LabelFor" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sub-option 2.2"
android:textStyle="bold" />
<EditText
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:autofillHints="no"
android:ems="10"
android:inputType="numberSigned"
android:minHeight="48dp"
android:text="3"
android:textAlignment="center"
tools:ignore="LabelFor" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sub-option 2.3"
android:textStyle="bold" />
<EditText
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:autofillHints="no"
android:ems="10"
android:inputType="numberSigned"
android:minHeight="48dp"
android:text="2"
android:textAlignment="center"
tools:ignore="LabelFor" />
</LinearLayout>
<View style="@style/Divider" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3"
android:textAlignment="center"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3.1"
android:textStyle="bold" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
tools:ignore="DuplicateSpeakableTextCheck" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3.2"
android:textStyle="bold" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3.3"
android:textStyle="bold" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp" />
<View style="@style/Divider" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 4"
android:textAlignment="center"
android:textSize="15sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.SwitchCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="Option 4.1" />
<androidx.appcompat.widget.SwitchCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="txt_option_4_2" />
<androidx.appcompat.widget.SwitchCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="txt_option_4_3" />
</LinearLayout>
</LinearLayout>
<!-- </ScrollView>-->
</androidx.constraintlayout.widget.ConstraintLayout>
如何在我的UI中正确添加卷轴?我也觉得我可以使我的代码变得更好,因此我对与之相关的任何建议都开放。
注意:我省略了ID和硬编码字符串,而不是在此处使用@String资源。
I'm new to Android layout design. My problem is that when trying to add a ScrollView to my layout it disappears the rest of my UI. In the following code, when I uncomment the ScrollView, it happens the aforementioned problem.
<?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"
tools:context=".activity.SettingActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:title="Toolbar Title" />
</LinearLayout>
<!-- <ScrollView-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent">-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="1dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 1"
android:textAlignment="center"
android:textSize="15sp"
android:textStyle="bold" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="193dp"
android:layout_height="48dp"
android:text="Radio 1" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="Radio 2" />
</RadioGroup>
<View style="@style/Divider" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 2"
android:textAlignment="center"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 2 Description"
android:textAlignment="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sub-option 2.1"
android:textStyle="bold" />
<EditText
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:autofillHints="no"
android:ems="10"
android:inputType="numberSigned"
android:minHeight="48dp"
android:text="2"
android:textAlignment="center"
tools:ignore="LabelFor" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sub-option 2.2"
android:textStyle="bold" />
<EditText
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:autofillHints="no"
android:ems="10"
android:inputType="numberSigned"
android:minHeight="48dp"
android:text="3"
android:textAlignment="center"
tools:ignore="LabelFor" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sub-option 2.3"
android:textStyle="bold" />
<EditText
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:autofillHints="no"
android:ems="10"
android:inputType="numberSigned"
android:minHeight="48dp"
android:text="2"
android:textAlignment="center"
tools:ignore="LabelFor" />
</LinearLayout>
<View style="@style/Divider" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3"
android:textAlignment="center"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3.1"
android:textStyle="bold" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
tools:ignore="DuplicateSpeakableTextCheck" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3.2"
android:textStyle="bold" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 3.3"
android:textStyle="bold" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp" />
<View style="@style/Divider" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Option 4"
android:textAlignment="center"
android:textSize="15sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.SwitchCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="Option 4.1" />
<androidx.appcompat.widget.SwitchCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="txt_option_4_2" />
<androidx.appcompat.widget.SwitchCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="txt_option_4_3" />
</LinearLayout>
</LinearLayout>
<!-- </ScrollView>-->
</androidx.constraintlayout.widget.ConstraintLayout>
How can I add properly a ScrollView to my UI? I also feel like I could make my code much better, so I'm open to any suggestion related to it.
Note: I have omitted ids and hardcoded strings rather than using @string resource here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与其向
linearlayout
提供边距和约束,不如将其移动到ScrollView
之类的:和更改
linearlayout
width and Heightth width and Heightth tomatch_parent < /code>和
wrap_content
。scrollview
只能有一个孩子。虽然,在您的情况下,这不是问题,而只是想提及它。Instead of giving margins and constraints to the
LinearLayout
, move them toScrollView
like this:and change
LinearLayout
width and height tomatch_parent
andwrap_content
respectively.ScrollView
can only have one child. Although, that's not a problem in your case but just wanna mention it out.这取决于您要如何安排root Confaintaintlayout的孩子的关系。如果将它们全部设置为所有,
那么它们是重叠的,而后来的一个隐藏了所有先前的。
因此,您应该将它们设置为不要使用App:layout_constraint ***属性相互重叠。
由于Stackoverflow并未设计为技术学习者的教程书,因此建议您找到约束layout的文档,然后首先阅读。
It depends on how you want to arrange the relation of the children of the root ConstraintLayout. If you set them all as
Then they are overlap and the later one hides all previous ones.
So, you should set them not to overlap each other by using app:layout_constraint*** attributes.
As StackOverflow is not designed as a tutorial book to technology learners, I suggest you find the document of ConstraintLayout and read it first.