Android ConstraintLayout中的组阻止设置ProgressBar可见性

Android ConstraintLayout中的组阻止设置ProgressBar可见性,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,我正在通过使用新的ConstraintLayout(1.1.2)来扁平化布局,但当ProgressBar位于组中时,我无法再控制它的可见性。下面是我的布局XML文件的最简单版本,它再现了这个问题: <android.support.constraint.ConstraintLayout android:layout_width="wrap_content" android:layout_height="wrap_content">

我正在通过使用新的ConstraintLayout(1.1.2)来扁平化布局,但当ProgressBar位于组中时,我无法再控制它的可见性。下面是我的布局XML文件的最简单版本,它再现了这个问题:

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <!-- This group prevents me from hiding ProgressBar -->
        <android.support.constraint.Group
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="imageLoadingSpinner" />

        <ProgressBar
            android:id="@+id/imageLoadingSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </android.support.constraint.ConstraintLayout>
为了清楚起见,如果我删除了该组,如下图所示,我可以将ProgressBar的可见性设置为
GONE

    <android.support.constraint.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <!-- Commenting out this group allows me to hide ProgressBar -->
        <!--<android.support.constraint.Group-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--app:constraint_referenced_ids="imageLoadingSpinner" />-->

        <ProgressBar
            android:id="@+id/imageLoadingSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </android.support.constraint.ConstraintLayout>

是不相关的。

我不认为这是一个问题,因为本质上这是
组的使用,根据:

此类控制一组引用小部件的可见性

此外:

组的可见性将应用于引用的小部件。这是一种方便的方法,可以轻松隐藏/显示一组小部件,而无需以编程方式维护该组小部件


因此,您必须设置组本身的可见性。我不知道您使用该组的目的是什么,因为您没有指定,但也许您应该重新构造以更好地利用它,或者完全摆脱它。

然而,正如前面所说的,您不能更改单个组的子组的可见性(因此,我觉得这个小部件没有多大的帮助和乐趣),始终可以设置其alpha属性

因此,如果您没有以任何方式弄乱要隐藏的视图的alpha值,您可以执行以下操作:

find(R.id.imageLoadingSpinner).setAlpha(0);

我找不到不这样做的理由,除了可能视图仍将被渲染,然后所有渲染的像素将转换为0-alpha,因此将变得不可见,浪费几个时钟周期,但在大多数情况下,将其视为一个问题将是一种夸张。

只要添加到各种解决方案中,您就可以扩展组并使其像您所期望的那样运行。如果您经常使用组,并且您希望在可见性方面有正常的亲子行为,那么会更方便一些

public class LayoutGroup extends Group {
    public LayoutGroup(Context context) {
        super(context);
    }

    public LayoutGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LayoutGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void updatePreLayout(ConstraintLayout container) {
        int visibility = this.getVisibility();
        float elevation = 0.0F;
        if (Build.VERSION.SDK_INT >= 21) {
            elevation = this.getElevation();
        }

        for(int i = 0; i < this.mCount; ++i) {
            int id = this.mIds[i];
            View view = container.getViewById(id);
            if (view != null) {
                // If the group is visible, let the child control visibility
                view.setVisibility(visibility == VISIBLE ? view.getVisibility() : visibility);
                if (elevation > 0.0F && Build.VERSION.SDK_INT >= 21) {
                    view.setElevation(elevation);
                }
            }
        }
    }
}
公共类布局组扩展组{
公共布局组(上下文){
超级(上下文);
}
公共布局组(上下文、属性集属性){
超级(上下文,attrs);
}
公共布局组(上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
}
@凌驾
public void updatePreLayout(ConstraintLayout容器){
int visibility=this.getVisibility();
浮标标高=0.0F;
如果(Build.VERSION.SDK_INT>=21){
高程=this.getElevation();
}
for(int i=0;i0.0F&&Build.VERSION.SDK\u INT>=21){
视图。设置高程(高程);
}
}
}
}
}
以下是基类(组)的代码供参考:

for(int i=0;i0.0F&&VERSION.SDK\u INT>=21){
视图。设置高程(高程);
}
}
}
然后在布局文件中,您当然可以使用自己的组件:

   <yourpackage.LayoutGroup
            android:id="@+id/group_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="view1,view2,view3"/>


我明白了,我不认为组的可见性会优先于子级,因为我正在转换的布局是线性布局而不是组,并且可见性逻辑工作正常。所以我想我只使用一个屏障而不是一个组。@MichaelOsofsky是的,视图侧面的屏障约束也可以起作用。这种行为似乎是错误和愚蠢的。谢谢你的正确答案。好主意,谢谢。我同意你的观点,群组小部件的用处要小得多,因为你无法独立于群组控制其子组件的可见性。我更喜欢使用setVisibility控制可见性的解决方案,因为它更易于维护(特别是,项目的未来开发人员可以比使用setAlpha的技巧更快地理解它)。谢谢你!“…因此,我觉得这个小部件没有那么有用和有趣”-你可以再说一遍!这种有点违反直觉的行为只花了我两个小时。。。
  for(int i = 0; i < this.mCount; ++i) {
        int id = this.mIds[i];
        View view = container.getViewById(id);
        if (view != null) {
            view.setVisibility(visibility);
            if (elevation > 0.0F && VERSION.SDK_INT >= 21) {
                view.setElevation(elevation);
            }
        }
    }
   <yourpackage.LayoutGroup
            android:id="@+id/group_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="view1,view2,view3"/>