Visibility properties in Android

Source: Internet
Author: User

Android development, most of the control has visibility this property, its properties are 3 respectively "visible", "invisible", "Gone". Used primarily to set the display and hiding of control controls.

(visible) 1)

XML file: android:visibility= "visible" Java code: view.setvisibility (view.visible);

2) not visible (invisible)

XML file: android:visibility= "Invisible" Java Code: view.setvisibility (view.invisible);

3) Hide (GONE)

XML file: android:visibility= "Gone" Java Code: view.setvisibility (View.gone);

To differentiate its function, the test demo is as follows:

Java code:
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:
<?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_margi Nbottom= "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>
Operation Result:

Code Explanation:

Two TextView for testing visible, invisible, gone effects

First TextView to test the display effect after the second textview is set to Visible,invisible,gone state

Second TextView, set to visible, invisible, gone

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

Test results

1) Click on the second button (BTN2) and set the second TextView (txt222) to invisible, which shows the following results:

2) Click on the second button ( btn3 ) and set the second TextView (txt222) to gone , which shows the following results:

3) Click on the second button ( btn1 ) and set the second TextView (txt222) to visible , showing the following effect:

Conclusion:

Visible: Setting controls Visible

Invisible: Setting controls not visible

Gone: Setting controls to hide

Invisible 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

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.