无法在约束组上设置可见性 [英] Can't set visibility on constraint group
问题描述
当我尝试在按钮单击上设置组的可见性时,它不会影响视图的可见性.使用com.android.support.constraint:constraint-layout:1.1.0-beta4.我尝试过将其设置为逐元素,但没有任何问题,但是该小组没有成功.
When i try to set the visibility of the group on button click,it doesn't affect the view's visibility.Using com.android.support.constraint:constraint-layout:1.1.0-beta4. I've tried setting it element-wise without problems,but no success with the group.
我的MainActivity.kt
private fun toggleLoginUI(show: Boolean) {
if (show) {
group.visibility = VISIBLE
} else {
group.visibility = INVISIBLE
}
}
fun onClick(view: View) {
when (view.id) {
R.id.button -> toggleLoginUI(true)
R.id.button4 -> toggleLoginUI(false)
}
}
我的activity_main.xml
<android.support.constraint.ConstraintLayout..
<TextView
android:id="@+id/textView"
... />
<TextView
android:id="@+id/textView2"
... />
<Button
android:id="@+id/button"
.../>
<Button
android:id="@+id/button4"
... />
<android.support.constraint.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:constraint_referenced_ids="textView,textView2" />
</android.support.constraint.ConstraintLayout>
推荐答案
对我来说,这似乎是个错误. GONE
可以,但是INVISIBLE
不能,我认为应该.除非有人可以发表我的想法错误的地方,否则可能值得提交错误报告. (我正在使用constraint-layout:1.1.0-beta4
.)
This looks like a bug to me. GONE
works but INVISIBLE
doesn't and I think it should. It may be worth a bug report unless someone can post where my thinking is wrong. (I am using constraint-layout:1.1.0-beta4
.)
同时,这是一种变通方法,可显式检索组中的ID并设置每个检索到的视图的可见性.
In the meantime, here is a work-around that explicitly retrieves the ids within the group and sets the visibility of each retrieved view.
在MainActivity.kt中
private fun toggleLoginUI(show: Boolean) {
if (show) {
setGroupVisibility(mLayout, group, Group.VISIBLE)
} else {
setGroupVisibility(mLayout, group, Group.INVISIBLE)
}
}
private fun setGroupVisibility(layout: ConstraintLayout, group: Group, visibility: Int) {
val refIds = group.referencedIds
for (id in refIds) {
layout.findViewById<View>(id).visibility = visibility
}
}
mLayout
是ConstraintLayout
.
更新:这是另一种变通办法,它利用更改为GONE
或从其更改为预期的效果这一事实:
Update: Here is another work-around that takes advantage of the fact that changing to/from GONE
works as expected:
private fun toggleLoginUI(show: Boolean) {
if (show) {
group.visibility = GONE
group.visibility = VISIBLE
} else {
group.visibility = GONE
group.visibility = INVISIBLE
}
}
这篇关于无法在约束组上设置可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!