如果某些视图可见性在约束布局中消失,如何将约束设置为不同的视图?

Ale*_*289 4 android android-layout android-constraintlayout

我的主要活动中有两个不同的工具栏。我一次只显示一个工具栏。如果一个工具栏可见性可见,另一个将消失。它由 if else 语句控制

这两个工具栏被称为:

  • 通用工具栏
  • 搜索工具栏

这两个身高不一样

我正在使用include在我的主要活动中附加工具栏。

这是我的主要活动的 xml

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Activities.MainActivity" android:id="@+id/constraintLayout_main_activity">


    <include
            android:id="@+id/include_toolbar_general"
            layout="@layout/include_toolbar_general"
            android:visibility="visible"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" android:layout_width="0dp"
            android:layout_height="wrap_content"/>

    <include
            android:id="@+id/include_toolbar_search"
            layout="@layout/include_toolbar_search"
            android:visibility="gone"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" android:layout_width="0dp"
            android:layout_height="wrap_content"/>


    <fragment
            android:id="@+id/nav_host_fragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
            app:layout_constraintTop_toBottomOf="@+id/include_toolbar_general"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:navGraph="@navigation/main_graph"
            app:defaultNavHost="true"/>


</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

如您所见,include_toolbar_general最初是可见的。并且它下面的片段具有约束Top_toBottomOfinclude_toolbar_general

问题是,当在某些条件下,include_toolbar_general将消失。并且片段将上升并且它似乎与include_toolbar_search(可见,如果通用工具栏消失)重叠。

我想制作,当include_toolbar_general可见性消失时,它下面的片段将具有约束Top_toBottomOfinclude_toolbar_search

怎么做 ?

Paw*_*ski 6

您可以设置Barrier用哪个工具目前正显示出底部对齐和约束的顶部FragmentBarrier

<androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="include_toolbar_general,include_toolbar_search" />
Run Code Online (Sandbox Code Playgroud)

在你的<fragment>变化中:

app:layout_constraintTop_toBottomOf="@+id/include_toolbar_general"
Run Code Online (Sandbox Code Playgroud)

到:

app:layout_constraintTop_toBottomOf="@+id/barrier"
Run Code Online (Sandbox Code Playgroud)