Visibility properties in Android the difference between visible, INVISIBLE, gone

Source: Internet
Author: User

Reprinted blog: http://blog.csdn.net/chindroid/article/details/8000713

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:

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Fill_parent"4android:layout_height= "Fill_parent"5android:orientation= "Vertical" >6<LinearLayout7Android:layout_width= "Fill_parent"8android:layout_height= "Wrap_content"9android:orientation= "Horizontal"Tenandroid:layout_marginbottom= "20dip" > One  A<TextView -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" theandroid:layout_weight= "1" -Android:background= "#F00" -android:text= "TextView1" -Android:textsize= "23SP" +android:visibility= "Visible"/> -  +<TextView AAndroid:id= "@+id/maintv2" atAndroid:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:layout_weight= "1" -Android:background= "#00F" -android:text= "TextView2" -Android:textsize= "23SP" inandroid:visibility= "Visible"/> -</LinearLayout> to      +<Button -Android:id= "@+id/mainbtn1" theAndroid:layout_width= "Fill_parent" *android:layout_height= "Wrap_content" $android:text= "TextView2 for Visible"Panax Notoginsengandroid:onclick= "Mianonclicklistener"/> -      the<Button +Android:id= "@+id/mainbtn2" AAndroid:layout_width= "Fill_parent" theandroid:layout_height= "Wrap_content" +Android:text= "TextView2 for Invisible" -android:onclick= "Mianonclicklistener"/> $      $<Button -Android:id= "@+id/mainbtn3" -Android:layout_width= "Fill_parent" theandroid:layout_height= "Wrap_content" -android:text= "TextView2 for Gone"Wuyiandroid:onclick= "Mianonclicklistener"/> the</LinearLayout>

The next three buttons as long as the properties of the visibility that control TextView

Java code:

1  Packagecom.chindroid.visibility;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.View;6 ImportAndroid.widget.TextView;7 8  Public classMainactivityextendsActivity {9     /**TextView2*/Ten     PrivateTextView mainTV2 =NULL; One      A @Override -      Public voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.main); -          -         //Initializing Data - InitData (); +     } -  +     /**methods for initializing controls*/ A     Private voidInitData () { atMainTV2 =(TextView) Findviewbyid (R.ID.MAINTV2); -     } -      -     /** - * How to respond to button click events in Mainactivity -      *  in      * @paramv -      */ to      Public voidMianonclicklistener (View v) { +         Switch(V.getid ()) { -              Caser.id.mainbtn1:{//Response Events for Button 1 the                 //Set TextView2 visible * maintv2.setvisibility (view.visible); $                  Break;Panax Notoginseng             } -              Caser.id.mainbtn2:{//Response Events for Button 2 the                 //set TextView2 not visible + maintv2.setvisibility (view.invisible); A                  Break; the             } +              Caser.id.mainbtn3:{//Response Events for Button 3 -                 //Set TextView2 Hide $ maintv2.setvisibility (view.gone); $                  Break; -             } -             default: the                  Break; -         }Wuyi     } the}

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

GONE: Setting Controls to 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, 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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.