SetVisibility 不适用于 ImageButton
因此,我一直在论坛中寻找如何做到这一点,但我发现没有任何效果。当我在图像按钮上调用 setVisibility() 时,该按钮不受影响。下面是 onCreate 方法中的代码,当我运行应用程序时,两个按钮都会显示。但是,如果我将属性硬编码到 xml 文件中,可见性就会发生变化。有什么想法为什么会发生这种情况吗?
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = new ImageButton(this);
btn1.setVisibility(GONE);
btn2 = new ImageButton(this);
btn2.setVisibility(GONE);
So I've been looking around in forums for how to do this, but nothing I have found has worked. When I call setVisibility() on my image button, the button is unaffected. Below is my code that is in the onCreate method, and both buttons are showing up when I run the application. However, if I were to hardcode the attribute into the xml file, the visibility does change. Any ideas why this is happening?
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = new ImageButton(this);
btn1.setVisibility(GONE);
btn2 = new ImageButton(this);
btn2.setVisibility(GONE);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将代码更改为:
并修改 main.xml:
Change your code to :
and modify your main.xml:
首先:您应该像这样使用 setVisibility 方法:
第二:
您已经创建了按钮,但是
您没有将它们添加到您的活动中
,(您的活动的内容是(R.layout.main
)尝试以下操作:
编辑:
super.onCreate(savedInstanceState);
FIRST : you shoud use the method setVisibility like this :
SECONDE:
you have created your buttons , but
you didn't add them to your activity
, ( the content of your activity is (R.layout.main
)try this :
EDIT :
super.onCreate(savedInstanceState);