2

当我尝试在按钮单击上设置组的可见性时,它不会影响视图的可见性。使用com.android.support.constraint:constraint-layout:1.1.0 -beta4。我已经尝试过将它设置为元素明智而没有问题,但是没有成功。无法设置约束组的可见性

我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> 
+0

具有看看[这里](https://stackoverflow.com/问题/ 44449495/toggle-visibility-of-chain-group-in-constraint-layout) –

+0

最新的问题给日志 –

+0

没有错误的log.it只是没有做任何事 – SS2095

回答

2

这看起来像是一个bug。 GONE作品,但INVISIBLE不,我认为它应该。除非有人可以在我的思维错误的地方发帖,否则可能值得一个错误报告。 (我正在使用constraint-layout:1.1.0-beta4。)

与此同时,这里是一个解决方法,显式检索组中的ID并设置每个检索视图的可见性。

在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 
    } 
} 

mLayoutConstraintLayout

更新:这里是另一个变通,它利用的事实,改变/从GONE按预期工作的优势:

private fun toggleLoginUI(show: Boolean) { 
    if (show) { 
     group.visibility = GONE 
     group.visibility = VISIBLE 
    } else { 
     group.visibility = GONE 
     group.visibility = INVISIBLE 
    } 
} 
+0

工作,但希望有一种方式,不需要通过元素循环。感谢答案。它得到解决。 – SS2095

+0

@ SS2095如果这是一个错误,那么你将不得不等待未来的版本。 (我认为这是一个错误。)如果你需要这个功能,这是最好的。 – Cheticamp

+0

@ SS2095增加备用解决方法来回答。无论如何, – Cheticamp

0

只需添加后续行,你可以改变它。 所以它可见。

group.visibility=ConstraintLayout.GONE 
+0

它的工作尝试它。 –

+1

它确实有效。 – SS2095

相关问题