Problem description: an image is displayed when it needs to be displayed; otherwise, it is hidden. When an image is hidden, no layout control is used. When the image is displayed, all the la s of the image are moved down sequentially. :
Click the button to display the image. Click again and the image disappears.
Solution:
View visibility allows you to view information online.
Use the property Android: visibility to control the layout file XML.
This property has three parameters: visible (visible) invisible (invisible) and gone
Gone and invisible are both invisible. However, gone does not occupy any layout space when hidden.
After understanding the above attributes and attribute parameters, you can design a listener for the button in the Java file. click the button to control the view visibility.
The complete code is provided below.
In addition, I have uploaded the project to csdn and can download it from anyone who needs it. Address: http://download.csdn.net/download/jenn_lian/4520407
XML file:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/Hello"/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "click it! "
Android: Id = "@ + ID/btn_mybuttom"
/>
<Imageview
Android: Id = "@ + ID/IMG"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
Android: src = "@ drawable/YouTube"
/>
<Textview
Android: text = "it is an example! "
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</Linearlayout>
Java file:
Package com. csdn. Blog;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. imageview;
/**
* The activity is to show the invisible layout when it is necessary
*
* @ Author Jenn
*
*/
Public class sumaryactivity extends activity {
Private button btn_mybutton;
Private imageview imgview_img;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Findview ();
Btn_mybutton.setonclicklistener (New onclicklistener (){
Public void onclick (view v ){
// I = 0, it is visible; I = 4, it is invisible; I = 0, it is gone;
Int I = 0;
I = imgview_img.getvisibility ();
If (I = 8 ){
Imgview_img.setvisibility (view. Visible );
} Else {
Imgview_img.setvisibility (view. Gone );
}
}
});
}
/**
* Find views by ID for each one
*/
Public void findview (){
Btn_mybutton = (button) findviewbyid (R. Id. btn_mybuttom );
Imgview_img = (imageview) findviewbyid (R. Id. IMG );
}
}
The following is a simple description of I. The getvisibility () method is used to obtain whether the current space is visible. If it is visible, the return value is 8, invisible but occupies space, and the return value is 4. Invisible does not occupy space, and the return value is 0.