Android scrollview消散了我的UI的其余部分

发布于 2025-02-03 19:29:05 字数 8583 浏览 4 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独 2025-02-10 19:29:06

与其向linearlayout提供边距和约束,不如将其移动到ScrollView之类的:

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="70dp"
    android:layout_marginEnd="10dp"
    android:layout_marginBottom="1dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <!-- Rest of the code -->

    </LinearLayout>

</ScrollView>

和更改linearlayout width and Heightth width and Heightth to match_parent < /code>和wrap_contentscrollview只能有一个孩子。虽然,在您的情况下,这不是问题,而只是想提及它。

Instead of giving margins and constraints to the LinearLayout, move them to ScrollView like this:

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="70dp"
    android:layout_marginEnd="10dp"
    android:layout_marginBottom="1dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <!-- Rest of the code -->

    </LinearLayout>

</ScrollView>

and change LinearLayout width and height to match_parent and wrap_content respectively. ScrollView can only have one child. Although, that's not a problem in your case but just wanna mention it out.

窝囊感情。 2025-02-10 19:29:06

这取决于您要如何安排root Confaintaintlayout的孩子的关系。如果将它们全部设置为所有,

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

那么它们是重叠的,而后来的一个隐藏了所有先前的。

因此,您应该将它们设置为不要使用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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文