首页 > 移动平台 > 详细

Android -- 加载布局

时间:2015-02-26 11:32:22      阅读:275      评论:0      收藏:0      [点我收藏+]

  如果在Activity中用到了别的Layout ,比如对话框等,你还要使用对话框中的组件,如TextView等,必须要先加载布局,然后才能使用里面的控件,

  如 :

    View view = View.inflate(this,R.layout.dialog,null);

    TextView textView = (TextView)view.findViewById(R.id.textView);  

    textView.setText("asafda");

    而你使用this.findViewById 则肯定会报错

 

    有三种方式可以生成LyaoutInflater

    LayoutInflater  inflater =  LayoutInflater.from(this);
  
    LayoutInflater   inflater  =  getLayoutInflater();
  
    LayoutInflater  inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
  
    然后调用inflate方法将xml布局文件转成View
  
    public  View  inflate(intresource,ViewGrouproot,booleanattachToRoot)
  
    在View类中,也有inflate方法
  
    public  static   View  inflate(Contextcontext,intresource,ViewGrouproot)

  

Android -- 加载布局

原文:http://www.cnblogs.com/SM-t/p/4300656.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!