我是 Android 布局設計的新手。我的問題是,當嘗試將ScrollView添加到我的布局時,它會消失我的 UI 的其余部分。在下面的代碼中,當我取消注釋 ScrollView 時,會發生上述問題。
<?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>
如何將 ScrollView 正確添加到我的 UI?我也覺得我可以讓我的代碼變得更好,所以我愿意接受任何與之相關的建議。
注意:我省略了 id 和硬編碼字串,而不是在這里使用 @string 資源。
uj5u.com熱心網友回復:
與其為 提供邊距和約束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寬度和高度分別更改為match_parent和wrap_content。ScrollView只能生一個孩子。雖然,在你的情況下這不是問題,但只是想提一下。
uj5u.com熱心網友回復:
這取決于您希望如何安排根 ConstraintLayout 的子項的關系。如果您將它們全部設定為
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
然后它們是重疊的,后一個隱藏了所有以前的。
因此,您應該使用 app:layout_constraint*** 屬性將它們設定為不相互重疊。
由于 StackOverflow 不是為技術學習者設計的教程書,我建議你先找到 ConstraintLayout 的檔案并閱讀它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/486624.html
