码迷,mamicode.com
首页 > 移动开发 > 详细

Android 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar

时间:2017-06-08 23:38:37      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:prot   message   ext   his   sdn   out   enter   line   logs   

package cc.testprogressdialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
/**
 * Demo描述:
 * 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar
 * 
 * 参考资料:
 * 1 http://blog.csdn.net/lfdfhl/article/details/18913435
 * 2 http://blog.csdn.net/lfdfhl/article/details/9299075
 * 3 http://rocye.iteye.com/blog/1258936
 *   Thank you very much
 *   
 * 如果在某个线性布局中动态添加一个控件,并且想让该控件在布局中居中.
 * 也可采用createProgressBar()方法中类似的方式
 *
 */
public class MainActivity extends Activity {
    private Context mContext;
    private ProgressBar mProgressBar;
    private ProgressDialog mProgressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //显示ProgressBar
        createProgressBar();
        //显示ProgressDialog
        //createProgressDialog();
    }
    
    private void createProgressBar(){
        mContext=this;
        //整个Activity布局的最终父布局,参见参考资料
        FrameLayout rootFrameLayout=(FrameLayout) findViewById(android.R.id.content);
        FrameLayout.LayoutParams layoutParams=
        new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.gravity=Gravity.CENTER;
        mProgressBar=new ProgressBar(mContext);
        mProgressBar.setLayoutParams(layoutParams);
        mProgressBar.setVisibility(View.VISIBLE);
        rootFrameLayout.addView(mProgressBar);
    }
    
    
    private void createProgressDialog(){
        mContext=this;
        mProgressDialog=new ProgressDialog(mContext);
        mProgressDialog.setMessage("...请您稍等...");
        mProgressDialog.show();
    }

}

 

Android 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar

标签:prot   message   ext   his   sdn   out   enter   line   logs   

原文地址:http://www.cnblogs.com/zhujiabin/p/6965005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!