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);
                }
            });        
  • 相关阅读:
    Junit初级编码(一)第一个Junit测试程序
    关于健身与健美
    spring入门(一)
    HTTP状态301、404、200、304分别表示什么意思
    预处理prepareStatement是怎么防止sql注入漏洞的?
    详解SESSION与COOKIE的区别
    Form表单中method为get和post的区别
    tomcat服务器配置及使用
    ubuntu 14.04 安装mysql server初级教程
    《电路学习第三天》 之 线性稳压电源的设计
  • 原文地址:https://www.cnblogs.com/Jaylong/p/progressdialog.html
Copyright © 2011-2022 走看看