【Java】view.setVisibility()的使用方法

view.setVisibility()方法在Java中对控件布局动态设置是否可见时使用,共有三个参数,在使用时可以直接用int代替参数,具体参数如下:

参数int说明
View.VISIBLE0可见,View会占有所分配的布局空间
View.INVISIBLE4不可见,View会占有所分配的布局空间
View.GONE8不可见,View不会占有所分配的布局空间

使用预览效果:

代码:


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TableLayout.LayoutParams;
import android.view.Gravity;

public class MainActivity extends Activity implements View.OnClickListener {
	TextView tv;
	LinearLayout ll;
	Button bt[]=new Button[3];
	String str[]={"VISIBLE","INVISIBLE","GONE"};
	@Override
	protected void onCreate(Bundle s) {
        super.onCreate(s);
		tv = new TextView(this);
		TextView tv1=new TextView(this);
		ll = new LinearLayout(this);
		ll.setLayoutParams(new LayoutParams(-1, -1));
		LayoutParams lp=new LayoutParams();
		lp.height = -2;
		lp.width = -2;
		LinearLayout tvl=new LinearLayout(this);
		ll.setGravity(Gravity.CENTER);
		tvl.setGravity(Gravity.CENTER);
		ll.setOrientation(1);
		tvl.setOrientation(1);
		tvl.setLayoutParams(lp);
		tvl.setBackgroundColor(Color.YELLOW);
		tvl.setPadding(50, 50, 50, 50);
		tv.setLayoutParams(lp);
		tv.setText("这是未设置的状态,即 View.VISIBLE");
		tv1.setLayoutParams(lp);
		tv1.setText("快点下面的按钮试试吧");
		tv.setBackgroundColor(Color.BLUE);
		tvl.addView(tv);
		tvl.addView(tv1);
		ll.addView(tvl);
		for (int i=0;i < 3;i++) {
			bt[i] = new Button(this);
			bt[i].setText(str[i]);
			bt[i].setId(-21000 - i);
			bt[i].setLayoutParams(lp);
			bt[i].setOnClickListener(this);
			ll.addView(bt[i]);
		}
		ll.setBackgroundColor(-1);

		setContentView(ll);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case -21000:
				tv.setVisibility(View.VISIBLE);
				tv.setText(str[0]);
				break;
			case -21001:
				tv.setVisibility(View.INVISIBLE);
				tv.setText(str[1]);
				break;
			case -21002:
				tv.setVisibility(View.GONE);
				tv.setText(str[2]);
				break;
		}
	}


}

预览图:

预览图

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = findViewById(R.id.list_view); List<ImportViewModel> data = readExcelFile(); // 调用您的 readExcelFile() 方法获取数据 ImportViewModelAdapter adapter = new ImportViewModelAdapter(data); listView.setAdapter(adapter); TextView textView = findViewById(R.id.text_type); textView.setVisibility(View.GONE); TextView textView1 = findViewById(R.id.text_name); textView1.setVisibility(View.GONE); TextView textView2 = findViewById(R.id.text_style); textView2.setVisibility(View.GONE); TextView textView3 = findViewById(R.id.text_inventory_num); textView3.setVisibility(View.GONE); RfApplication.toneGenerator = new ToneGenerator(streamType, percantageVolume); btn_update = findViewById(R.id.btn_update); rvList = findViewById(R.id.list_view); rvList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { ShowData item = (ShowData) adapterView.getItemAtPosition(i); if (!isStarted) { modeDialog(item); } else { CustomToast.showLoad(MainActivity.this, getString(R.string.please_stop_read), 2000); } return true; } }); // showDataList = new ArrayList<>(); //adapter = new StockInListViewAdapter(this, showDataList); rvList.setAdapter(adapter); }java.lang.IllegalStateException: Cannot get a numeric value from a text cell怎么解决
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值