Android Visible属性学习

在写Android的过程中发现有个属性,它有三种状态:VISIBLE、INVISIBLE、GONE,通过名字发现,三者似乎有一些区别,于是自己写了测试代码来验证。
首先是visiblity_test.xml文件

"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
>
    "match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
    >
        "1"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="TextView1"
                android:background="#ffffff"
                android:textColor="#000000"
                android:gravity="center"
        />
        "@+id/TextView2"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:text="TextView2"
                android:background="#fff000"
                android:textColor="#000000"
                android:gravity="center"
        />
    
    

效果图如下
Android Visible属性学习_第1张图片

然后是MyActivity.java

package com.imudges.AndroidStudy;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyActivity extends Activity implements Button.OnClickListener {
    /**
     * Called when the activity is first created.
     */

    /**
     * for visiblity test
     * */
    public TextView mTextView = null;
    public Button btn_vis;
    public Button btn_invis;
    public Button btn_gone;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.visiblity_test);
        initData();
        btn_vis.setOnClickListener(this);
        btn_invis.setOnClickListener(this);
        btn_gone.setOnClickListener(this);
    }

    private void initData(){
        btn_vis = (Button) findViewById(R.id.btn_vis);
        btn_invis = (Button) findViewById(R.id.btn_invis);
        btn_gone = (Button) findViewById(R.id.btn_gone);
        mTextView = (TextView) findViewById(R.id.TextView2);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_vis: {
                mTextView.setVisibility(View.VISIBLE);
                break;
            }
            case R.id.btn_invis:{
                mTextView.setVisibility(View.INVISIBLE);
                break;
            }
            case R.id.btn_gone:{
                mTextView.setVisibility(View.GONE);
                break;
            }
        }
    }
}

有兴趣的同志可以试试~

你可能感兴趣的:(Android,学习)