On the visibility attribute in Android _android

Source: Internet
Author: User
Tags visibility

In Android development, most controls have visibility this property, with 3 attributes of "visible", "invisible", "Gone". Used primarily to set the display and hide of control controls.
1) Visible (visible)
XML file: android:visibility= "visible"
Java code: view.setvisibility (view.visible);

2) not seen (invisible)
XML file: android:visibility= "Invisible"
Java code: view.setvisibility (view.invisible);

3) Hidden (GONE)
XML file: android:visibility= "Gone"
Java code: view.setvisibility (View.gone);

To differentiate its effect, the test demo is as follows:
Java code:

Copy Code code as follows:

# public class Mainactivity extends activity {
# private TextView txt222 = null;
#
# @Override
# protected void OnCreate (Bundle savedinstancestate) {
# super.oncreate (savedinstancestate);
# Setcontentview (R.layout.main);
#
# txt222 = (TextView) Findviewbyid (r.id.txt2222);
#
# Findviewbyid (R.ID.BTN1). Setonclicklistener (listener);
# Findviewbyid (R.ID.BTN2). Setonclicklistener (listener);
# Findviewbyid (R.ID.BTN3). Setonclicklistener (listener);
#     }
#
# View.onclicklistener listener = new View.onclicklistener () {
# @Override
# public void OnClick (View v) {
# switch (V.getid ()) {
# Case R.ID.BTN1:
# txt222.setvisibility (view.visible);
# break;
#
# Case R.ID.BTN2:
# txt222.setvisibility (view.invisible);
# break;
#
# Case R.ID.BTN3:
# txt222.setvisibility (View.gone);
# break;
#             }
#         }
#     };
# }

XML code:
Copy Code code as follows:

<?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:layout_marginbottom= "20dip"
android:orientation= "Horizontal" >

<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
Android:background= "#F00"
android:text= "1111"
android:visibility= "Visible"/>

<textview
Android:id= "@+id/txt2222"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
Android:background= "#0F0"
android:text= "2222"
android:visibility= "Visible"/>
</LinearLayout>

<button
Android:id= "@+id/btn1"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "2222 VISIBLE"/>

<button
Android:id= "@+id/btn2"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "2222 Invisible"/>

<button
Android:id= "@+id/btn3"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "2222 GONE"/>

</LinearLayout>

Run Result:



Code Explanation:
Two TextView for testing visible, invisible, gone effects

The first TextView is used to test the display of the second TextView after it is set to the Visible,invisible,gone state

The second TextView, which is set to visible, invisible, gone

Three button for operation of the second TextView state is visible, invisible, gone

Test results
1) Click on the second button (BTN2), set the second TextView (txt222) as invisible, the display effect is as follows:



2) Click on the second button (btn3), set the second TextView (txt222) as gone, the display effect is as follows:



3) Click on the second button (btn1), set the second TextView (txt222) as visible, the display effect is as follows:


Conclusion:
visible      : Setting controls visible
invisible   : Setting Control not visible
gone       &NBSP: Setting control Hide

invisible and gone  The main differences are:
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 retain 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.