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.