Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android利用代碼在屏幕中間位置顯示ProgressDialog和ProgressBar

Android利用代碼在屏幕中間位置顯示ProgressDialog和ProgressBar

編輯:關於Android編程

MainActivity如下:

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();
	}

}

main.xml如下:



    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved