Article source: blog.csdn.net/chindroid/a…

In Android development, most controls have the property visibility, and its properties are “Visible”, “Invisible”, and “gone”. It is used to set the display and hide of control controls. Some people may wonder what the difference is between Invisible and Gone? So, let’s look at this with this question:

This is set in the XML file and Java code as follows:

 

 

Visible (visible)

XML file: Android :visibility=”visible”

Java code: view.setvisibility (view.visible);

 

Invisible

XML file: Android :visibility=”invisible”

Java code: view.setvisibility (view.invisible);

 

GONE

XML file: Android: Visibility =”gone”

Java code: view.setvisibility (view.gone);

 

 

In order to distinguish the three, I built a Dome to demonstrate the difference:

The XML file:

[html]  view plain copy

  1. The < LinearLayout XMLNS: android = “schemas.android.com/apk/res/and…
  2.     android:layout_width=”fill_parent”  
  3.     android:layout_height=”fill_parent”  
  4.     android:orientation=”vertical”>  
  5.     <LinearLayout  
  6.         android:layout_width=”fill_parent”  
  7.         android:layout_height=”wrap_content”  
  8.         android:orientation=”horizontal”  
  9.         android:layout_marginBottom=”20dip” >  
  10.   
  11.         <TextView  
  12.             android:layout_width=”wrap_content”  
  13.             android:layout_height=”wrap_content”  
  14.             android:layout_weight=”1″  
  15.             android:background=”#F00″  
  16.             android:text=”TextView1″  
  17.             android:textSize=”23sp”  
  18.             android:visibility=”visible” />  
  19.   
  20.         <TextView  
  21.             android:id=”@+id/mainTV2″  
  22.             android:layout_width=”wrap_content”  
  23.             android:layout_height=”wrap_content”  
  24.             android:layout_weight=”1″  
  25.             android:background=”#00F”  
  26.             android:text=”TextView2″  
  27.             android:textSize=”23sp”  
  28.             android:visibility=”visible” />  
  29.     </LinearLayout>  
  30.       
  31.     <Button   
  32.         android:id=”@+id/mainBtn1″  
  33.         android:layout_width=”fill_parent”  
  34.         android:layout_height=”wrap_content”  
  35. Android: text = “TextView2 for VISIBLE”
  36.         android:onClick=”mianOnClickListener”/>  
  37.       
  38.     <Button   
  39.         android:id=”@+id/mainBtn2″  
  40.         android:layout_width=”fill_parent”  
  41.         android:layout_height=”wrap_content”  
  42. Android: text = “TextView2 to cause”
  43.         android:onClick=”mianOnClickListener”/>  
  44.       
  45.     <Button   
  46.         android:id=”@+id/mainBtn3″  
  47.         android:layout_width=”fill_parent”  
  48.         android:layout_height=”wrap_content”  
  49. Android: text = “TextView2 is GONE”
  50.         android:onClick=”mianOnClickListener”/>  
  51. </LinearLayout>  

The last three buttons are properties that control the visibility of the TextView

Java code:

[java]  view plain copy

  1. package com.chindroid.visibility;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.TextView;  
  7.   
  8. public class MainActivity extends Activity {  
  9.     /** TextView2 */  
  10.     private TextView mainTV2 = null;  
  11.       
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17. // Initialize the data
  18.         initData();  
  19.     }  
  20.   
  21. /** How to initialize the controller */
  22.     private void initData() {  
  23.         mainTV2 = (TextView)findViewById(R.id.mainTV2);  
  24.     }  
  25.       
  26. / * *
  27. * The method in MainActivity that responds to button click events
  28.      *  
  29.      * @param v 
  30. * /
  31.     public void mianOnClickListener(View v){  
  32.         switch (v.getId()){  
  33. Case R.i.MAINbtn1 :{// The response event of button 1
  34. // Make TextView2 visible
  35.                 mainTV2.setVisibility(View.VISIBLE);  
  36.                 break;  
  37.             }  
  38. Case R.i.MAINbtn2 :{// The response event for button 2
  39. // Make TextView2 invisible
  40.                 mainTV2.setVisibility(View.INVISIBLE);  
  41.                 break;  
  42.             }  
  43. Case R.i.MAINbtn3 :{// The response event of button 3
  44. // Set TextView2 to hide
  45.                 mainTV2.setVisibility(View.GONE);  
  46.                 break;  
  47.             }  
  48.             default:  
  49.                 break;  
  50.         }  
  51.     }  
  52. }  

Both TextViews are visible as soon as the program starts

When we click the first button to set the TextView2visibility property to INVISIBLE, the procedure is as follows:

When we click the third button and set TextView2visibility to GONE, the procedure looks like the following:

When we click the first button and set TextView2visibility to VISIBLE, TextView2 will appear again, as shown below:

 

See from the above demonstration

VISIBLE: Makes the control VISIBLE

INVISIBLE: Makes controls INVISIBLE

GONE: Hides the control

 

The main difference between INVISIBLE and GONE is that when the control visibility attribute is INVISIBLE, the interface retains the space occupied by the view control. When the control property is GONE, the interface does not retain the space occupied by the View control.

\