Differences between visibility, invisible, and gone in Android

Source: Internet
Author: User

In Android development, most controls have the visibility attribute, which has three attributes: "visible", "invisible", and "gone ". It is mainly used to set the display and hide of control controls. Some may wonder what is the difference between invisible and gone ??? So let's take a look at this question:

In the XML file and Java code, it is set as follows:

Visible)

XML file: Android: visibility = "visible"

Java code: view. setvisibility (view. Visible );

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 created a dome demo. First I went to the dome code and the demo to know the differences between them:

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: 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 is visible" Android: onclick = "mianonclicklistener"/> <button Android: Id = "@ + ID/mainbtn2" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "textview2 is invisible" Android: onclick = "mianonclicklistener"/> <button Android: Id = "@ + ID/mainbtn3" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "textview2 is gone" Android: onclick = "mianonclicklistener"/> </linearlayout>

The following three buttons only control the visibility attribute of textview:

Package COM. example. androidtestdemo; 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 the data initdata ();}/** method of initializing the control */private void initdata () {maintv2 = (textview) findviewbyid (R. id. maintv2);} public void mianonclicklistener (view v) {Switch (v. GETID () {case R. id. mainbtn1: {// RESPONSE event of Button 1 // set textview2 to see maintv2.setvisibility (view. visible); break;} case R. id. mainbtn2: {// RESPONSE event of Button 2 // set textview2 invisible maintv2.setvisibility (view. invisible); break;} case R. id. mainbtn3: {// RESPONSE event of button 3 // set textview2 to hide maintv2.setvisibility (view. gone); break;} default: break ;}}}

Because both textviews are visible when the program starts.
When we click the 1st button and set the textview2visibility attribute to invisible, the program is as follows:


When we click the 3rd button and set the textview2visibility attribute to gone, the program is as follows:


When we click the 1st button and set the textview2visibility attribute to visible, textview2 is displayed again, as shown in:


We can see from the above demonstration
Visible: Set control visibility
Invisible: sets the control to be invisible.
Gone: Set the control to hide
The main difference between invisible and gone is that when the visibility attribute of the control is invisible, the interface retains the space occupied by the view control; when the control attribute is gone, the page does not reserve the space occupied by the view control.

Related Article

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.