android和view相关的东西

findViewById(R.id.xxxx),把xml里面的控件根据id绑定到类成员变量,适用于普通控件和布局

 

LayoutInflater,用于把布局xml里面的东西实例化(会重新实例化,和findViewById不同),sample代码如下

 

LayoutInflater inflator = getLayoutInflater();

//注意,这里参数是layout,不是id
RelativeLayout layout = (RelativeLayout)inflator.inflate(R.layout.activity_main, null);
        
 TextView aTextView = new TextView(this);
aTextView.setWidth(100);
aTextView.setHeight(100);
aTextView.setText("asdasdasdasd");
layout.addView(aTextView);
        

//采用LayoutInflater的需要重新设置布局才有效果。
setContentView(layout);

 

附三种使用LayoutInflater的方法

LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.main, null);
 
LayoutInflater inflater = getLayoutInflater();  
View layout = inflater.inflate(R.layout.main, null);
 
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
View layout = inflater.inflate(R.layout.main, null); 

 

 

转载于:https://www.cnblogs.com/ziyouchutuwenwu/archive/2013/05/22/3093761.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值