解决android动态设置控件不可见无效的问题

本文探讨了在Android开发中使用XML布局代码设置Group和Button组件的可见性问题。通过对比Group和Button在动态设置不可见时的行为差异,揭示了willNotDraw属性的作用及其对视图绘制的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml布局代码

<androidx.constraintlayout.widget.Group
        android:id="@+id/group1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="btn3,btn1"
        />

动态设置Group不可见,加上group.setWillNotDraw(true);才有效 属性 (willNotDraw默认为false)

Group group = findViewById(R.id.group1);
group.setWillNotDraw(true);
group.setVisibility(View.INVISIBLE);

而动态设置Button不可见,不用加setWillNotDraw(true);即有效

Button button = findViewById(R.id.btn1);
button.setVisibility(View.INVISIBLE);

奇怪的是,当设置button.setWillNotDraw(true);时,第一次运行“不可见无效”,后面再运行“不可见有效”(暂且记下,过后探究)

Button button1 = findViewById(R.id.btn1);
button1.setWillNotDraw(false);
button1.setVisibility(View.INVISIBLE);

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值