Can I hide an image button on a layout, (dimensions and background) until a call to set visible?

你离开我真会死。 提交于 2019-12-03 13:53:10

问题


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);?


回答1:


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.




回答2:


Try setting the visibility to GONE




回答3:


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




回答4:


Set Visibility property of Imageview like this in java

imgView.setVisibility(View.VISIBLE);
imgView.setVisibility(View.INVISIBLE);
imgView.setVisibility(View.GONE);

Or like this in XML

android:visibility="visible"
android:visibility="gone"
android:visibility="invisible"

Result for each will be like this



来源:https://stackoverflow.com/questions/4249237/can-i-hide-an-image-button-on-a-layout-dimensions-and-background-until-a-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!