in Android development, most of the controls have the visibility attribute, with 3 properties of "visible", "invisible", and "Gone" respectively. Used primarily to set the display and hiding of control controls . Some people may wonder what is the difference between invisible and gone??? So, let's look at the following with this question:
It is set in the XML file and Java code as follows:
Viewable (visible)
XML file: android:visibility= "visible"
Java code: view.setvisibility (view.visible);
Not visible (invisible)
XML file: android:visibility= "Invisible"
Java code: view.setvisibility (view.invisible);
Hidden (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 will know their differences:
XML file:
[HTML]View PlainCopy
- <? 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:orientation="Horizontal"
- android:layout_marginbottom="20dip" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:background="#F00"
- android:text="TextView1"
- android:textsize="23SP"
- android:visibility="visible" />
- <TextView
- android:id="@+id/maintv2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android: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_parent"
- android:layout_height="wrap_content"
- android:text="TextView2 for visible"
- android:onclick="Mianonclicklistener"/>
- <Button
- android:id="@+id/mainbtn2"
- android:layout_width="fill_parent"
- android:layout_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>
The next three buttons as long as the properties of the visibility that control TextView
Java code:
[Java]View PlainCopy
- 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 mainTV2 = null;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- //Initialize data
- InitData ();
- }
- /** Methods for initializing controls * /
- private void InitData () {
- MainTV2 = (TextView) Findviewbyid (R.ID.MAINTV2);
- }
- /**
- * How to respond to button click events in Mainactivity
- *
- * @param v
- */
- public void Mianonclicklistener (View v) {
- switch (V.getid ()) {
- case r.id.mainbtn1:{ //button 1 Response event
- //Set TEXTVIEW2 visible
- Maintv2.setvisibility (view.visible);
- Break ;
- }
- case r.id.mainbtn2:{ //Button 2 response event
- //Set TEXTVIEW2 not visible
- Maintv2.setvisibility (view.invisible);
- Break ;
- }
- case r.id.mainbtn3:{ //Button 3 response event
- //Set TEXTVIEW2 hide
- Maintv2.setvisibility (View.gone);
- Break ;
- }
- Default:
- Break ;
- }
- }
- }
Since the start of the program two TextView are visible
When we click the 1th button and set the Textview2visibility property to invisible, the program looks like this:
When we click the 3rd button and set the Textview2visibility property to gone, the program looks like this:
When we click the 1th button again and set the Textview2visibility property to visible, the TextView2 appears again, as shown in:
By the above demonstration
Visible: Setting controls Visible
INVISIBLE: Setting controls not visible, will occupy pits
GONE: Setting controls hidden, not occupying pits
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, while the control property is gone, the interface does not preserve the space occupied by the view control.
Visibility properties in Android the difference between visible, INVISIBLE, gone