约束布局 - 组可见性在动态模块内不起作用

2024-03-21

有没有人遇到过问题ConstraintLayout团体能见度? 我在用着ConstraintLayout1.1.3 我正在设置组的可见性XML布局和java代码。但它不会改变可见性状态。它总是可见的。

这是布局文件

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

这可能是什么原因?

UPDATE:

我忘了提及我正在动态模块内使用此布局。我还在基本模块中使用它进行了测试,它按预期工作,但不在动态模块中。所以最后我弄清楚了这个问题的原因。另外,当我调试代码并评估此表达式(group.getVisibility == View.GONE)时,它给了我 TRUE。(即使组内的视图仍然可见)

任何建议表示赞赏。


找到了解决此问题的方法,我希望这能为其他人节省时间。这很奇怪,但这个问题的原因是使用ConstraintLayout - 动态模块内的组。

所以修复是在java代码中,一旦你获得了对组的引用,然后使用int数组设置引用id,如下所示,

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});

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

约束布局 - 组可见性在动态模块内不起作用 的相关文章

随机推荐