【问题标题】:How to handle the following case for ConstraintLayout, when one of the targets of layout_constraint visibility changed to View.GONE当 layout_constraint 可见性的目标之一更改为 View.GONE 时,如何处理 ConstraintLayout 的以下情况
【发布时间】:2018-04-30 15:31:26
【问题描述】:

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

因此,我尝试了以下方法

<?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>

输出如下。

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

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

我希望拥有的是

克服的方法之一是将title_text_view(黄色)的可见性更改为View.GONE后,我需要使用Java代码手动更新app:layout_constraintTop_toBottomOf的body_text_view(蓝色)-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代码,不嵌套布局?

【问题讨论】:

  • 你想让视图不可见还是消失?
  • 保持视图消失。
  • 好的,可以做到,但问题是你必须保持 pin 视图底部与黄色文本视图对齐,这是因为如果你将可见性设置为消失,并且在再次设置可见后它将与你的黄色布局混合.

标签: android android-constraintlayout


【解决方案1】:

你可以使用一个屏障,它在 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

【讨论】:

  • 不需要使用1.1.0-beta1,只要有发布1.1.0即可。
  • 哦,是的,我的错误。改了
【解决方案2】:

您可以使用以下代码最接近您的要求,而无需添加任何额外代码:

<?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"
    android:visibility="visible"
    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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:background="#ff0000"
    android:text="Pin"
    android:textColor="#ffffff"
    app:layout_constraintEnd_toEndOf="@+id/body_text_view" />

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

或者如果你想做一些额外的 xml 代码,你可以使用 guidelines 中的 guidelines 功能来做这样的事情constraintLayout:

<android.support.constraint.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"
android:layout_margin="30dp">

<TextView
    android:id="@+id/title_text_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:background="#ffd600"
    android:padding="5dp"
    android:text="Title\nTitle"
    android:visibility="visible"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    app:layout_constraintEnd_toStartOf="@+id/pin_text_view"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.09" />

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

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

   </android.support.constraint.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2016-12-15
    • 2020-08-23
    • 2011-01-13
    • 2021-03-12
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多