• Inflate()


     LayoutInflater作用是将layout的xml布局文件实例化为View类对象,LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。

       二.获得 LayoutInflater 实例的三种方式

              1.LayoutInflater inflater = getLayoutInflater();                //调用Activity的getLayoutInflater()

              2.LayoutInflater inflater = LayoutInflater.from(this);       

              3.LayoutInflater inflater = (LayoutInflater)Context.getSystemService(LAYOUT_INFLATER_SERVICE);

              

              其实,这三种方式本质是相同的,从源码中可以看出:

              getLayoutInflater():

              Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:

                

    public PhoneWindow(Context context) {  
            super(context);  
            mLayoutInflater = LayoutInflater.from(context);  
    }  

             可以看出它其实是调用 LayoutInflater.from(context)。

             LayoutInflater.from(context):

    复制代码
    public static LayoutInflater from(Context context) {   
        LayoutInflater LayoutInflater =   
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
        if (LayoutInflater == null) {   
            throw new AssertionError("LayoutInflater not found.");   
        }   
        return LayoutInflater;   
    } 
    复制代码

           可以看出它其实调用 context.getSystemService()。

          结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。

      因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:
      
      View view=View.inflate(this,R.layout.dialog_layout,null);
      
      TextView dialogTV=(TextView)view.findViewById(R.id.dialog_tv);
      
      dialogTV.setText("abcd");

    package com.bivin;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements OnClickListener {
    
    private Button button;
    
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
    
    showCustomDialog();
    }
    
    public void showCustomDialog() {
    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    Context mContext = MainActivity.this;
    
    LayoutInflater inflater = (LayoutInflater) mContext
    .getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_dialog, null);
    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("Hello, Welcome to Mr Wei's blog!");
    ImageView image = (ImageView) layout.findViewById(R.id.image);
    image.setImageResource(R.drawable.icon);
    builder = new AlertDialog.Builder(mContext);
    builder.setView(layout);
    alertDialog = builder.create();
    alertDialog.show();
    }
    }
    View Code

  • 相关阅读:
    高性能css动画
    关于thinkphp验证码的那些事
    DOM对象的属性
    关于data属性的一些常见的处理方式
    webstorm快捷键整理
    javascript模块化编程
    2016年5月30日上午(传智Bootstrap笔记六(图片样式))
    Bootstrap列排序
    2016年5月29日晚上(传智Bootstrap笔记五(表单2))
    2016年5月29日晚上(传智Bootstrap笔记四(栅格系统 ))
  • 原文地址:https://www.cnblogs.com/xiangtailiang/p/3379553.html
Copyright © 2020-2023  润新知