在约束布局内的线性布局中将元素置于其他元素之下
Place elements under others in Linear Layout inside Constraint Layout
我正在尝试将一组元素放在我的 LinearLayout 中的列中。问题是它不识别像 "layout_below" 这样的指令(它说它是无效的):结果是这样的:
我期待的结果是第一行的第一个 TextView 和 Spinner,第二行的 "Release" TextView 和 EditText,以及第三行的其他三个元素。这是代码:
<?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:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="49dp"
android:layout_marginLeft="49dp"
android:layout_marginEnd="49dp"
android:layout_marginRight="49dp"
android:text="Search"
android:textColor="#ffffff"
android:textSize="@dimen/_20ssp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/search_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="20dp"
android:background="@drawable/search_layout"
android:ems="10"
android:fontFamily="@font/nunito_bold"
android:hint="Search here"
android:inputType="textPersonName"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="16sp"
app:layout_constraintEnd_toStartOf="@id/search_btn"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="@id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<ImageButton
android:id="@+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@color/cardview_shadow_end_color"
android:paddingTop="17dp"
app:layout_constraintBottom_toTopOf="@id/checkbox_adv"
app:layout_constraintEnd_toEndOf="@id/textView2"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="@+id/search_field"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:srcCompat="@mipmap/search_button" />
<CheckBox
android:id="@+id/checkbox_adv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Advanced search"
android:layout_marginEnd="@dimen/_30sdp"
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toTopOf="@id/hiddenTab"
app:layout_constraintEnd_toEndOf="@+id/search_btn"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="@+id/search_field"
app:layout_constraintTop_toBottomOf="@+id/search_field" />
<LinearLayout
android:id="@+id/hiddenTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
android:layout_marginTop="@dimen/_7sdp"
app:layout_constraintBottom_toTopOf="@id/popular_rc_view"
app:layout_constraintEnd_toEndOf="@id/search_btn"
app:layout_constraintStart_toStartOf="@id/search_field"
app:layout_constraintTop_toBottomOf="@+id/checkbox_adv"
tools:visibility="visible">
<!-- LINE 1 -->
<TextView
android:id="@+id/textGenre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Genre:"
android:textColor="@color/colorPrimary"
android:textAlignment="gravity" />
<Spinner
android:id="@+id/spinnerGenre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_margin="10dp"
android:textColor="@color/colorPrimary"
android:entries="@array/genre_list" />
<!-- END OF LINE 1 -->
<!-- LINE 2 -->
<TextView
android:id="@+id/textYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Release:"
android:layout_below="@+id/genresTextView"
android:textColor="@color/colorPrimary"
android:textAlignment="gravity" />
<EditText
android:id="@+id/releaseYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_margin="10dp"
android:textColor="@color/colorPrimary" />
<!-- END OF LINE 2 -->
<!-- LINE 3 -->
<TextView
android:id="@+id/textVote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Rating:"
android:layout_below="@+id/genresTextView"
android:textColor="@color/colorPrimary"
android:textAlignment="gravity" />
<Spinner
android:id="@+id/spinnerOperator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_margin="10dp"
android:textColor="@color/colorPrimary"
android:entries="@array/operator_list" />
<Spinner
android:id="@+id/spinnerVote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_margin="10dp"
android:textColor="@color/colorPrimary"
android:entries="@array/vote_list" />
<!-- END OF LINE 3 -->
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/popular_rc_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/hiddenTab" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:scaleType="center"
android:src="@drawable/recent_icon"
android:text="Most Recent"
android:visibility="invisible"
app:backgroundTint="@color/design_default_color_on_primary"
app:elevation="6dp"
app:layout_constraintBottom_toTopOf="@+id/fab2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:maxImageSize="35dp"
app:pressedTranslationZ="12dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:scaleType="center"
android:src="@drawable/rated_icon"
android:text="Most Rated"
android:visibility="invisible"
app:backgroundTint="@color/design_default_color_background"
app:elevation="6dp"
app:layout_constraintBottom_toTopOf="@+id/fab1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:maxImageSize="35dp"
app:pressedTranslationZ="12dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:scaleType="center"
android:src="@drawable/pop_icon"
android:text="Most Popular"
android:visibility="invisible"
app:backgroundTint="@color/design_default_color_background"
app:elevation="6dp"
app:layout_constraintBottom_toTopOf="@+id/fab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:maxImageSize="35dp"
app:pressedTranslationZ="12dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="20dp"
android:scaleType="centerInside"
android:src="@drawable/filter"
app:backgroundTint="@color/colorPrimary"
app:elevation="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:maxImageSize="35dp"
app:pressedTranslationZ="12dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
无法识别属性 android:layout_below
,因为它是 RelativeLayout
的属性,LinearLayout
没有那种属性。
要获得您想要的行为,LinearLayout
不适合您,ViewGroup
因为它只允许水平或垂直堆叠子视图,不能混合水平排列和垂直排列。
您应该使用 ConstraintLayout
(更好)或 RelativeLayout
(更差)来获得该行为。
此外,由于您的 parent 已经是 ConstraintLayout
,您不需要嵌套的 ViewGroup
hiddenTab
来安排该位置的视图。如果只是添加 hiddenTab
来控制其可见性,请记住您可以使用 ContraintLayout
的 Group
来控制它。
我正在尝试将一组元素放在我的 LinearLayout 中的列中。问题是它不识别像 "layout_below" 这样的指令(它说它是无效的):结果是这样的:
我期待的结果是第一行的第一个 TextView 和 Spinner,第二行的 "Release" TextView 和 EditText,以及第三行的其他三个元素。这是代码:
<?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:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="49dp"
android:layout_marginLeft="49dp"
android:layout_marginEnd="49dp"
android:layout_marginRight="49dp"
android:text="Search"
android:textColor="#ffffff"
android:textSize="@dimen/_20ssp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/search_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="20dp"
android:background="@drawable/search_layout"
android:ems="10"
android:fontFamily="@font/nunito_bold"
android:hint="Search here"
android:inputType="textPersonName"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textSize="16sp"
app:layout_constraintEnd_toStartOf="@id/search_btn"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="@id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<ImageButton
android:id="@+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@color/cardview_shadow_end_color"
android:paddingTop="17dp"
app:layout_constraintBottom_toTopOf="@id/checkbox_adv"
app:layout_constraintEnd_toEndOf="@id/textView2"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="@+id/search_field"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:srcCompat="@mipmap/search_button" />
<CheckBox
android:id="@+id/checkbox_adv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Advanced search"
android:layout_marginEnd="@dimen/_30sdp"
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toTopOf="@id/hiddenTab"
app:layout_constraintEnd_toEndOf="@+id/search_btn"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="@+id/search_field"
app:layout_constraintTop_toBottomOf="@+id/search_field" />
<LinearLayout
android:id="@+id/hiddenTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
android:layout_marginTop="@dimen/_7sdp"
app:layout_constraintBottom_toTopOf="@id/popular_rc_view"
app:layout_constraintEnd_toEndOf="@id/search_btn"
app:layout_constraintStart_toStartOf="@id/search_field"
app:layout_constraintTop_toBottomOf="@+id/checkbox_adv"
tools:visibility="visible">
<!-- LINE 1 -->
<TextView
android:id="@+id/textGenre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Genre:"
android:textColor="@color/colorPrimary"
android:textAlignment="gravity" />
<Spinner
android:id="@+id/spinnerGenre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_margin="10dp"
android:textColor="@color/colorPrimary"
android:entries="@array/genre_list" />
<!-- END OF LINE 1 -->
<!-- LINE 2 -->
<TextView
android:id="@+id/textYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Release:"
android:layout_below="@+id/genresTextView"
android:textColor="@color/colorPrimary"
android:textAlignment="gravity" />
<EditText
android:id="@+id/releaseYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_margin="10dp"
android:textColor="@color/colorPrimary" />
<!-- END OF LINE 2 -->
<!-- LINE 3 -->
<TextView
android:id="@+id/textVote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Rating:"
android:layout_below="@+id/genresTextView"
android:textColor="@color/colorPrimary"
android:textAlignment="gravity" />
<Spinner
android:id="@+id/spinnerOperator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_margin="10dp"
android:textColor="@color/colorPrimary"
android:entries="@array/operator_list" />
<Spinner
android:id="@+id/spinnerVote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_margin="10dp"
android:textColor="@color/colorPrimary"
android:entries="@array/vote_list" />
<!-- END OF LINE 3 -->
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/popular_rc_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/hiddenTab" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:scaleType="center"
android:src="@drawable/recent_icon"
android:text="Most Recent"
android:visibility="invisible"
app:backgroundTint="@color/design_default_color_on_primary"
app:elevation="6dp"
app:layout_constraintBottom_toTopOf="@+id/fab2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:maxImageSize="35dp"
app:pressedTranslationZ="12dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:scaleType="center"
android:src="@drawable/rated_icon"
android:text="Most Rated"
android:visibility="invisible"
app:backgroundTint="@color/design_default_color_background"
app:elevation="6dp"
app:layout_constraintBottom_toTopOf="@+id/fab1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:maxImageSize="35dp"
app:pressedTranslationZ="12dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:scaleType="center"
android:src="@drawable/pop_icon"
android:text="Most Popular"
android:visibility="invisible"
app:backgroundTint="@color/design_default_color_background"
app:elevation="6dp"
app:layout_constraintBottom_toTopOf="@+id/fab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:maxImageSize="35dp"
app:pressedTranslationZ="12dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="20dp"
android:scaleType="centerInside"
android:src="@drawable/filter"
app:backgroundTint="@color/colorPrimary"
app:elevation="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:maxImageSize="35dp"
app:pressedTranslationZ="12dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
无法识别属性 android:layout_below
,因为它是 RelativeLayout
的属性,LinearLayout
没有那种属性。
要获得您想要的行为,LinearLayout
不适合您,ViewGroup
因为它只允许水平或垂直堆叠子视图,不能混合水平排列和垂直排列。
您应该使用 ConstraintLayout
(更好)或 RelativeLayout
(更差)来获得该行为。
此外,由于您的 parent 已经是 ConstraintLayout
,您不需要嵌套的 ViewGroup
hiddenTab
来安排该位置的视图。如果只是添加 hiddenTab
来控制其可见性,请记住您可以使用 ContraintLayout
的 Group
来控制它。