In Android development, most controls have visibility this property, with 3 attributes of "visible", "invisible", "Gone". Used primarily to set the display and hide of control controls. Some people may wonder what the difference between invisible and gone is??? So, we take this question and look at the following:
It is set in the XML file and Java code as follows:
Visible (visible)
XML file: android:visibility= "visible"
Java code: view.setvisibility (view.visible);
Not seen (invisible)
XML file: android:visibility= "Invisible"
Java code: view.setvisibility (view.invisible);
Hide (GONE)
XML file: android:visibility= "Gone"
Java code: view.setvisibility (View.gone);
In order to distinguish the three, I built a dome to demonstrate, first on the dome code, after the demonstration to know their differences:
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"
; <linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:orientatio n= "Horizontal" android:layout_marginbottom= "20dip" > <textview android:layout_width= "wrap_content" "Android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:background=" #F00 "and
roid:text= "TextView1" android:textsize= "23sp" android:visibility= "visible"/> <textview Android:id= "@+id/maintv2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an
droid:layout_weight= "1" android:background= "#00F" android:text= "TextView2" android:textsize= "23SP" Android:visibility= "VISible "/> </LinearLayout> <button android:id=" @+id/mainbtn1 "android:layout_width=" fill_p Arent "android:layout_height=" wrap_content "android:text=" TextView2 for Visible "android:onclick=" Mianonclickli Stener "/> <button android:id= @+id/mainbtn2" android:layout_width= "Fill_parent" android:layou t_height= "Wrap_content" android:text= "TextView2 for Invisible" android:onclick= "Mianonclicklistener"/> ;
Button android:id= "@+id/mainbtn3" android:layout_width= "fill_parent" android:layout_height= "Wrap_content"
android:text= "TextView2 for Gone" android:onclick= "Mianonclicklistener"/> </LinearLayout>
Back three button as long as it is the visibility property of the control TextView
Java code:
Package com.chindroid.visibility;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.TextView;
public class Mainactivity extends activity {/** TextView2/private TextView = null;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Initialize data initdata ();
/** the method to initialize the control * * private void InitData () {mainTV2 = (TextView) Findviewbyid (R.ID.MAINTV2); /** * mainactivity In response to button click event * * @param v */public void Mianonclicklistener (View v) {s Witch (V.getid ()) {Case r.id.mainbtn1:{//button 1 response Event//settings TextView2 visible maintv2.setvisibility (view.vi
sible);
Break
Case r.id.mainbtn2:{//button 2 response Event//settings TextView2 not visible maintv2.setvisibility (view.invisible);
Break Case r.id.mainbtn3:{//PressButton 3 Response Event//Set TextView2 hidden maintv2.setvisibility (View.gone);
Break
} Default:break;
}
}
}
Since the program starts two TextView are visible
When we click on the 1th button and set the Textview2visibility property to invisible, the program looks like the following image:
When we click on the 3rd button and set the Textview2visibility property to gone, the program looks like the following image:
When we click on the 1th button and the Textview2visibility property is set to visible, the TextView2 is rendered again, as shown in the following figure:
From the demo above
VISIBLE: Set Control visible
Invisible: Set Control not visible
GONE: Set Control Hide
The main difference between invisible and gone is that when the control visibility property is invisible, the interface retains the space occupied by the view control, and the interface does not retain the space that the view control occupies when the control property is gone.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.