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

Source: Internet
Author: User

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:

<?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:orie ntation= "Horizontal" android:layout_marginbottom= "20dip" > <textview android:layout_width= " Wrap_content "android:layout_height=" Wrap_content "android:layout_weight=" 1 "android:back Ground= "#F00" android:text= "TextView1" android:textsize= "23sp" android:visibility= "Visibl            E "/> <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:te Xt= "TextView2 for Invisible" android:onclick= "Mianonclicklistener"/> <button android:id= "@+id/main Btn3 "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:

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 (); }/** initializes the control's method */private void InitData () {mainTV2 = (TextView) Findviewbyid (R.ID.MAINTV2);} /** * mainactivity Response button Click event Method * * @param v */public void Mianonclicklistener (View v) {switch (V.getid ()) {case R.ID.MAINBT n1:{//Button 1 Response Event//settings TextView2 visible maintv2.setvisibility (view.visible); Case r.id.mainbtn2:{//Button 2 response Event//settings TextView2 not visible maintv2.setvisibility (view.invisible); Case r.id.mainbtn3:{//Button 3 response Event//settings TextView2 hide maintv2.setvisibility (view.gone); Default:break;}}}

Note that the two TextView weights are set to 1.


Since the start of the program two TextView are visible

When we click the 2nd 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

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.