使用LayoutInflater添加一个布局引用

LayoutInflater 与 xml 的<include/>的区别,至今没搞清楚,下面记录一下LayoutInflater引用一个布局并立即显示呈现的方法:

private void initView(Context context){
        LayoutInflater inflater;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.top_opt_bar, null);
        this.addContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }
R.layout.top_opt_bar是要引用的布局xml,
LayoutParams.MATCH_PARENT是全宽,
LayoutParams.WRAP_CONTENT是内容高。
在onCreate方法用调用
initView(this);

即可立即呈现出来哦~

 
 
posted @ 2013-09-28 17:43  朝野布告  阅读(278)  评论(0编辑  收藏  举报