我可以隐藏布局上的图像按钮(尺寸和背景),直到调用设置可见吗?
我的 xml 布局之一中有一个隐藏图像按钮,其背景设置为可绘制图像。我将可见性设置为不可见,因为我只想偶尔显示图像。问题是,即使未显示可绘制对象,图像按钮仍然占用我们的空间 - 有没有办法隐藏背景图像,并使其尺寸为 0,直到我调用它在我的主类中显示?
谢谢!
编辑:那么在我的 xml 中,我该怎么写呢?
<ImageButton android:id="@+id/myimage" android:visibility="invisible"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:background="@drawable/my_image"></ImageButton>
我希望图像始终消失,除非发生某种条件,然后我希望图像在该条件下可见。因此,在我的 xml 中,我需要将其设置为 GONE,但在我的条件语句中,我会说:
myimage.setVisibility(SHOW);?
I have a hidden image button in one of my xmls layouts, with a background set to a drawable image. I set the visibility to invisible, as I only want the image to display every once in a while. The problem is, even if the drawable isn't shown, the image button still takes us space - Is there a way to hide the background image, and make it's dimensions 0 until I call on it to be shown in my main class?
Thanks!
Edit: So in my xml, how would I write that?
<ImageButton android:id="@+id/myimage" android:visibility="invisible"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:background="@drawable/my_image"></ImageButton>
I want to have the image always gone, unless a certain condition occurs, I then want the image to be visible under that one condition. So in my xml I would need to be set to GONE, but in my conditional statement so I say something like:
myimage.setVisibility(SHOW);?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将可见性设置为
GONE
Try setting the visibility to
GONE
不要将宽度/高度设置为零,那很丑。除非您更改可见性设置,否则视图将始终占用空间。这是您想要的代码:
myImageButton.setVisibility(View.GONE);
View.GONE - 不可见,不占用空间
View.INVISIBLE - 不可见,但仍然占用空间
View.VISIBLE - 使用它将其恢复
Don't set the width/height to zero, that's ugly. The view will always take up the space, unless you change the visibility setting. This is the code you want:
myImageButton.setVisibility(View.GONE);
View.GONE - invisible, takes up no space
View.INVISIBLE - invisible, but still takes up space
View.VISIBLE - use this to bring it back
所有视图,包括ImageButton,都继承自android.view.View,因此它们都有一个可以设置的可见性属性。不要设置可绘制资源的可见性,而是在 ImageButton 上设置它。
All views, including ImageButton, inherit from android.view.View, so they all have a visibility attribute that can be set. Instead of setting the visibility of the drawable resource, set it on the ImageButton.
在java中像这样设置Imageview的可见性属性
或者在XML中像这样设置
每个结果将像这样
Set Visibility property of Imageview like this in java
Or like this in XML
Result for each will be like this