Constraint遇到的那些坑之Android View设置visible无效的问题

  • 发现问题
    • Talk is cheap.Show me your code.
    • 解决方法

发现问题

近期在开发界面的时候,需要做Android的控件显示隐藏的操作,发现在代码中设置visible属性不生效,再看代码确确实实也执行了。发现问题很诡异,排查了很久终于解决了。

Talk is cheap.Show me your code.

当当当当~

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    xmlns:app="http://schemas.android.com/apk/res-auto">    <Button        android:id="@+id/button_1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Action1"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <Button        android:id="@+id/button_2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Action2"        app:layout_constraintTop_toBottomOf="@id/button_1"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent" />    <Button        android:id="@+id/button_3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Action3"        app:layout_constraintTop_toBottomOf="@id/button_2"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent" />    <androidx.constraintlayout.widget.Group        android:id="@+id/new_function"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:constraint_referenced_ids="button_1,button_2,button_3" /></androidx.constraintlayout.widget.ConstraintLayout>
package com.example.jetpack_demoimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.Viewimport android.widget.Buttonclass DemoActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.demo_layout)        findViewById<Button>(R.id.button_1).setOnClickListener {            findViewById<Button>(R.id.button_1).visibility = View.GONE        }    }}

我们执行后发现,效果并不理想。本来想上视频,但是csdn不支持直接上传,还得传到优酷,bilibili之类的网站,用链接的方式显示。算了,不麻烦了,想看一下效果的同学可以粘贴代码直接到自己工程里运行效果。

点击ACTION1按钮理想效果应该是隐藏掉ACTION2的,但是没有隐藏。

解决方法

不知道你们有没有发现,我这里引用里ConstrintLayout布局中的Group控件,对,没错,就是他搞的鬼。
凡是在Group控件的ids属性中包含的id,显示结果永远跟Group一致。只要把需要单独设置显示隐藏的控件id移出单独处理即可。

问题解决!!

更多相关文章

  1. Android:shape设置corners(圆角)不能正常显示问题
  2. 常用控件的使用方法(参考书籍'第一行代码')
  3. Android(安卓)平滑和立体翻页效果1
  4. [置顶] Android的Activity屏幕切换动画(一)-左右滑动切换
  5. 【android编程】 第二讲-这是好的一个开始
  6. Android新特性之CardView的简单使用
  7. Android之相机的使用
  8. 【Android(安卓)界面效果22】Android的Tab与TabHost
  9. android布局属性使用说明和一些开发经验

随机推荐

  1. Android中EventBus事件总线发送网络请求
  2. Android音频开发(3):如何播放一帧音频
  3. Android各种访问权限Permission详解
  4. android 的线程模型和AsyncTask
  5. Android(安卓)GPS学习笔记—HAL实现
  6. Android(安卓)Power Management
  7. Android绘图之绘制太极图
  8. Android(安卓)AsyncTask实现异步任务的执
  9. Android(安卓)Studio 常见 Build 问题 Er
  10. Android(安卓)HAL 开发 (1)