zoukankan      html  css  js  c++  java
  • android中LayoutInflater的用法及progressDialog的使用

          在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

                具体作用:

                               1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

                               2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

    在使用时候有三种方法:

                           //LayoutInflater inflater = LayoutInflater.from(context);
                           //LayoutInflater inflater=getLayoutInflater();
                             LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    

    请看下面的详细代码:注意在onCreate方法里面

        Button btn = new Button(this);
            btn.setText("点击加载");
            //LayoutInflater inflater = LayoutInflater.from(this);
            
    //LayoutInflater inflater=getLayoutInflater();
            LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);     
            RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.activity_main, null);
            view.addView(btn);
            setContentView(view);//设置当前的布局为view

    ProgressDialog的用法:

    有两种:1.重写Activitiy里面的onCreateDialog,

                    调用的时候,使用Activity的showDialog(0);

    public Dialog onCreateDialog(int id) {
            ProgressDialog pd = new ProgressDialog(this);
            pd.setTitle("进度条");//设置标题
            pd.setCancelable(true);//设置是否可以按回退键取消
            pd.setIndeterminate(true);//设置为不明确
            pd.setMessage("正在运行...");//设置内容
            return pd;
        }

                   2.直接创建出来

    btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {            
                    // TODO Auto-generated method stub
                    
    //showDialog(0);
                    ProgressDialog.show(MainActivity.this, "进度条", "正在加载,请稍等...",true,true);
                }
            });        
  • 相关阅读:
    Best Time to Buy and Sell Stock I II III
    数据挖掘算法面试题
    C# 从CIL代码了解委托,匿名方法,Lambda 表达式和闭包本质
    ASP.NET MVC 5
    net破解一(反编译,反混淆-剥壳,工具推荐)
    面试题及相关参考答案
    Linux 查看内核版本命令的相关说明
    c# 获取应用程序exe文件路径及退出应用程序的几种方法
    C# WebBrowser设置代理
    c# combobox控件的使用
  • 原文地址:https://www.cnblogs.com/Jaylong/p/progressdialog.html
Copyright © 2011-2022 走看看