Visibility attributes 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.

1) visible)
XML file: Android: visibility = "visible"
Java code: view. setvisibility (view. Visible );

2) 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 functions, the test demo is as follows:

Java code:

public class MainActivity extends Activity {private TextView txt222 = null;@Overrideprotected 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() {@Overridepublic 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_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>

Running result:

Code explanation:

Two textviews are used to test the effect of visible, invisible, and gone.

The first textview is used to test the display effect after the second textview is set to visible, invisible, and gone.

The second textview, which is used to set to visible, invisible, and gone.

Three buttons are used to operate the second textview in the visible, invisible, and gone statuses.

Test Results

1) Click the second button (Btn2), Set the second textview (txt222)Invisible, The display effect is as follows:

2) Click the second button (Btn3), Set the second textview (txt222)Gone, The display effect is as follows:

3) Click the second button (Btn1), Set the second textview (txt222)Visible, The display effect is as follows:

Conclusion:

Visible: Set control visibility
Invisible: sets the control to be invisible.
Gone: Set the control to hide

The main differences between invisible and gone are:

When the visibility attribute of the control is invisible, the interface retains the space occupied by the view control;

When the control property is gone, the interface does not reserve the space occupied by the view control.

Source code download

Reference recommendations:

Android layout attributes

Android animations

Android manifest. XML structure

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.