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:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <linearlayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="Horizontal"
  10. android:layout_marginbottom="20dip" >
  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. <TextView
  20. android:id="@+id/maintv2"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_weight="1"
  24. android:background="#00F"
  25. android:text="TextView2"
  26. android:textsize="23SP"
  27. android:visibility="visible" />
  28. </linearlayout>
  29. <Button
  30. android:id="@+id/mainbtn1"
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:text="TextView2 for visible"
  34. android:onclick="Mianonclicklistener"/>
  35. <Button
  36. android:id="@+id/mainbtn2"
  37. android:layout_width="fill_parent"
  38. android:layout_height="wrap_content"
  39. android:text="TextView2 for Invisible"
  40. android:onclick="Mianonclicklistener"/>
  41. <Button
  42. android:id="@+id/mainbtn3"
  43. android:layout_width="fill_parent"
  44. android:layout_height="wrap_content"
  45. android:text="TextView2 for Gone"
  46. android:onclick="Mianonclicklistener"/>
  47. </linearlayout>


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

Java code:

[Java]View PlainCopy
  1. Package com.chindroid.visibility;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.view.View;
  5. Import Android.widget.TextView;
  6. Public class Mainactivity extends Activity {
  7. /** TextView2 * /
  8. private TextView mainTV2 = null;
  9. @Override
  10. public void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.main);
  13. //Initialize data
  14. InitData ();
  15. }
  16. /** Methods for initializing controls * /
  17. private void InitData () {
  18. MainTV2 = (TextView) Findviewbyid (R.ID.MAINTV2);
  19. }
  20. /** 
  21. * How to respond to button click events in Mainactivity
  22. *
  23. * @param v
  24. */
  25. public void Mianonclicklistener (View v) {
  26. switch (V.getid ()) {
  27. case r.id.mainbtn1:{ //button 1 Response event
  28. //Set TEXTVIEW2 visible
  29. Maintv2.setvisibility (view.visible);
  30. Break ;
  31. }
  32. case r.id.mainbtn2:{ //Button 2 response event
  33. //Set TEXTVIEW2 not visible
  34. Maintv2.setvisibility (view.invisible);
  35. Break ;
  36. }
  37. case r.id.mainbtn3:{ //Button 3 response event
  38. //Set TEXTVIEW2 hide
  39. Maintv2.setvisibility (View.gone);
  40. Break ;
  41. }
  42. Default:
  43. Break ;
  44. }
  45. }
  46. }


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

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.