如何在约束布局中对齐线性布局
Posted
技术标签:
【中文标题】如何在约束布局中对齐线性布局【英文标题】:How to align a Linear Layout in a Constraint Layout 【发布时间】:2020-06-28 23:13:00 【问题描述】:我正在尝试在我的约束布局中添加一个线性布局。我知道这不应该这样做,但我要解释原因:我想让线性布局默认为“不可见”。只要用户按下它上面的复选框,我就会让这个布局对用户可见。问题是我不知道如何在复选框和回收视图之间进行设置。我尝试使用 app:layout_constraintTop_toBottomOf 之类的命令,但结果如下:
即使指向复选框的箭头正确,它也会锚定在布局的顶部。这是我的代码,我该如何改进它?
<?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_
android:layout_>
<TextView
android:id="@+id/textView2"
android:layout_
android:layout_
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_
android:layout_
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="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="parent"
app:layout_constraintTop_toTopOf="@+id/search_btn" />
<ImageButton
android:id="@+id/search_btn"
android:layout_
android:layout_
android:layout_marginTop="8dp"
android:background="@color/cardview_shadow_end_color"
android:paddingTop="17dp"
app:layout_constraintEnd_toEndOf="parent"
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_
android:layout_
android:text="Advanced search"
android:textColor="@color/colorPrimary"
android:layout_marginLeft="@dimen/_15sdp"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="@+id/search_field"
app:layout_constraintTop_toBottomOf="@+id/search_field"/>
<LinearLayout
android:id="@+id/hiddenTab"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintTop_toBottomOf="@+id/checkbox_adv"
android:layout_
android:layout_
android:orientation="horizontal" >
<TextView
android:id="@+id/textCountry"
android:layout_
android:layout_
android:layout_weight="1"
android:text="" />
<EditText
android:id="@+id/editCustom"
android:layout_
android:layout_
android:layout_weight="1.5"
android:inputType="text"
android:textSize="18sp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/popular_rc_view"
android:layout_
android:layout_
android:layout_marginTop="20dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkbox_adv">
</androidx.recyclerview.widget.RecyclerView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab3"
android:layout_
android:layout_
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:scaleType="center"
android:src="@drawable/recent_icon"
android:visibility="invisible"
android:text="Most Recent"
app:backgroundTint="@color/design_default_color_on_primary"
app:elevation="6dp"
app:maxImageSize="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/fab2"
app:pressedTranslationZ="12dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab2"
android:layout_
android:layout_
android:layout_marginTop="18dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:scaleType="center"
android:text="Most Rated"
android:src="@drawable/rated_icon"
android:visibility="invisible"
app:backgroundTint="@color/design_default_color_background"
app:elevation="6dp"
app:maxImageSize="35dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/fab1"
app:pressedTranslationZ="12dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab1"
android:layout_
android:layout_
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:scaleType="center"
android:src="@drawable/pop_icon"
android:visibility="invisible"
app:backgroundTint="@color/design_default_color_background"
app:elevation="6dp"
app:maxImageSize="35dp"
android:text="Most Popular"
app:layout_constraintBottom_toTopOf="@+id/fab"
app:layout_constraintEnd_toEndOf="parent"
app:pressedTranslationZ="12dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_
android:layout_
android:layout_marginEnd="16dp"
android:layout_marginRight="@dimen/_23sdp"
android:layout_marginBottom="@dimen/_20sdp"
android:scaleType="centerInside"
android:src="@drawable/filter"
app:backgroundTint="@color/colorPrimary"
app:elevation="6dp"
app:maxImageSize="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:pressedTranslationZ="12dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
【参考方案1】:试试这个:
<?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_
android:layout_>
<TextView
android:id="@+id/textView2"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
android:text="Advanced search"
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_
android:layout_
android:orientation="horizontal"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@id/popular_rc_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkbox_adv"
tools:visibility="visible">
<TextView
android:id="@+id/textCountry"
android:layout_
android:layout_
android:layout_weight="1"
android:gravity="center"
android:text=""
android:textAlignment="gravity" />
<EditText
android:id="@+id/editCustom"
android:layout_
android:layout_
android:layout_weight="1.5"
android:inputType="text"
android:textSize="18sp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/popular_rc_view"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_
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" />
将可见性设置为已消失,回收站视图会自动调整到约束条件。
【讨论】:
【参考方案2】:正如你所说,你想把它放在 checkbox 和 RecyclerView 之间, 将底部约束添加到您的 线性布局 并更改高度
app:layout_constraintBottom_toTopOf="@+id/popular_rc_view
android:layout_height to wrap_content
也在您的 RecyclerView 中 改变这个
app:layout_constraintTop_toBottomOf="@+id/checkbox_adv"
到
app:layout_constraintTop_toBottomOf="@+id/hiddenTab"
【讨论】:
以上是关于如何在约束布局中对齐线性布局的主要内容,如果未能解决你的问题,请参考以下文章