android - 当 layout_constraint 可见性的目标之一更改为 View.GONE 时,如何处理 ConstraintLayout 的以下情况

标签 android android-constraintlayout

我想避免嵌套布局,以提高我的应用性能。

因此,我尝试了以下方法

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_margin="30dp"
    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=".MainActivity">

    <TextView
        android:id="@+id/title_text_view"
        android:background="#ffd600"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Title\nTitle"
        app:layout_constraintEnd_toStartOf="@+id/pin_text_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/pin_text_view"
        android:background="#ff0000"
        android:textColor="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Pin" />

    <TextView
        android:id="@+id/body_text_view"
        android:background="#304ffe"
        android:textColor="#ffffff"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/title_text_view"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="Body" />

</android.support.constraint.ConstraintLayout>

输出如下。

enter image description here

在运行时,我可能会将 title_text_view(黄色)的可见性更改为 View.GONE

但是,它看起来像下面这样。我不希望 pin_text_view(红色)被 body_text_view(蓝色)覆盖

enter image description here

我想要的是

enter image description here

克服的方法之一是在将title_text_view(黄色)的可见性更改为View.GONE后,我需要使用Java代码手动更新 body_text_view 的 app:layout_constraintTop_toBottomOf(蓝色)- Android : How to programatically set layout_constraintRight_toRightOf "parent"

来自

<TextView
    android:id="@+id/body_text_view"
    ...
    app:layout_constraintTop_toBottomOf="@+id/title_text_view"

<TextView
    android:id="@+id/body_text_view"
    ...
    app:layout_constraintTop_toBottomOf="@+id/pin_text_view"

但是,这会使我的代码更难维护。

有没有更简单的方法,不涉及 Java 代码,也没有嵌套布局?

最佳答案

你可以使用屏障,它在 ConstraintLayout 1.1 版本中可用

因此,如果您使用的是旧版本的 ConstraintLayout,请将 build.gradle 中的 ConstraintLayout 的依赖项更改为

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

或更高版本。

然后将屏障添加到您的布局中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_margin="30dp"
    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=".MainActivity">

    <TextView
        android:id="@+id/title_text_view"
        android:background="#ffd600"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Title\nTitle"
        app:layout_constraintEnd_toStartOf="@+id/pin_text_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/pin_text_view"
        android:background="#ff0000"
        android:textColor="#ffffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Pin" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="title_text_view,pin_text_view" />

    <TextView
        android:id="@+id/body_text_view"
        android:background="#304ffe"
        android:textColor="#ffffff"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/barrier"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="Body" />

</android.support.constraint.ConstraintLayout>

有关更多信息,请参阅此 link

关于android - 当 layout_constraint 可见性的目标之一更改为 View.GONE 时,如何处理 ConstraintLayout 的以下情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50095178/

相关文章:

android - ConstraintLayout 中的 ScrollView 滚动错误

Android 将宽度设置为 ConstraintLayout.LayoutParams.MATCH_PARENT 给出 -1

android - 单击 EditText 时 View 不会一直向上滚动

android - 将数据写入 assets 目录下的 .properties 文件

android - Android 中的 Firebase 数据库引用

android - 在不同的 GLSurfaceView 之间共享 GLES20 上下文和纹理?

android - Android Studio 视觉布局编辑器中的严重延迟

android - RecyclerView 裁剪底部数据

Android 音频控制前进和后退

java - 如何在 Android 中连接或合并两个或多个视频文件?