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例如以下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="在屏幕中间位置显示ProgressDialog和ProgressBar" android:layout_centerHorizontal="true" /> </RelativeLayout>
版权声明:本文博客原创文章。博客,未经同意,不得转载。