组可见性在动态模块内不起作用

组可见性在动态模块内不起作用

本文介绍了约束布局-组可见性在动态模块内不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在 ConstraintLayout 组可见性方面遇到问题吗?
我正在使用 ConstraintLayout 1.1.3,并且在两个 XML 中设置组的可见性布局和Java代码。但这不会更改可见性状态。它总是可见的。

Has anyone experienced issues with ConstraintLayout group visibility?I'm using ConstraintLayout 1.1.3 and I'm setting the visibility of group in both XML layout and java code. But it does not change the visibility status. It is always visible.

这是布局文件

 <?xml version="1.0" encoding="utf-8"?>
 <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">

<ImageView
    android:id="@+id/iv_message_icon_activity_dictionary"
    android:layout_width="@dimen/message_icon_width"
    android:layout_height="@dimen/message_icon_height"
    android:layout_marginBottom="@dimen/message_text_view_margin_top"
    android:contentDescription="@string/app_name"
    android:src="@drawable/icon_error"
    app:layout_constraintBottom_toTopOf="@+id/tv_message_activity_dictionary"
    app:layout_constraintEnd_toEndOf="@id/tv_message_activity_dictionary"
    app:layout_constraintStart_toStartOf="@id/tv_message_activity_dictionary" />

<TextView
    android:id="@+id/tv_message_activity_dictionary"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginStart="20dp"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="@dimen/medium_text_size"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="@string/msg_no_internet" />

<Button
    android:id="@+id/btn_try_again_activity_dictionary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/try_again_button_margin_top"
    android:background="@drawable/rounded_button"
    android:minHeight="30dp"
    android:minWidth="@dimen/try_again_button_width"
    android:padding="10dp"
    android:text="@string/btn_try_again"
    android:textAllCaps="false"
    android:textColor="@color/white"
    android:textSize="@dimen/text_size_5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tv_message_activity_dictionary" />

<android.support.constraint.Group
    android:id="@+id/group_component_activity_dictionary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:constraint_referenced_ids="btn_try_again_activity_dictionary,tv_message_activity_dictionary,iv_message_icon_activity_dictionary" />

</android.support.constraint.ConstraintLayout>

这可能是什么原因?

更新:

我忘了提到我在动态模块中使用此布局。我也在基本模块中使用它进行了测试,它可以按预期运行,但在动态模块中却没有。所以最后我找出了问题的原因。同样,当我调试代码并评估此表达式(group.getVisibility == View.GONE)时,它给出了TRUE(即使组内的视图仍然可见)

任何建议都会受到赞赏。

Any suggestion is appreciated.

推荐答案

找到了解决此问题的方法,希望这可以节省时间给别人奇怪,但是此问题的原因是在动态模块内使用 ConstraintLayout-Group。

Found a fix for this issue and I hope this will save time for someone else. It's strange but the reason for this issue is using the ConstraintLayout - Group, inside a dynamic module.

因此,此修复程序在Java代码中,一旦获得对组的引用,然后使用如下所示的int数组设置引用ID,

So the fix is in java code, once you got a reference to the group then set the reference ids using int array like below,

Group group = findViewById(R.id.group);
group.setReferencedIds(new int[]{R.id.btn_try_again_activity_dictionary, R.id.tv_message_activity_dictionary, R.id.iv_message_icon_activity_dictionary});

此后,它会按预期工作。因此,无需设置组中单个视图的可见性。

After this, it works as expected. Therefore no need to set visibility of individual views in the group.

这篇关于约束布局-组可见性在动态模块内不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:17