怎么改变progressDialog消息字体字号
今天在论坛看到有人问怎么更改progressDialog的字体,调查了一下,终于搞清楚了,现在分享下。
(一)先把完整代码贴出来:
/** Called when the activity is first created. */
private static final int DLG_SHOW = 0;
private TextView mTextView;
private Button mButton, btnDialog;
private EditText mEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//dialog
btnDialog = (Button) findViewById(R.id.Button01);
btnDialog.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
showDialog(DLG_SHOW);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == DLG_SHOW) {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("loading");
dialog.setCancelable(true);
return dialog;
}
return super.onCreateDialog(id);
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
View v = dialog.getWindow().getDecorView();
setDialogText(v);
super.onPrepareDialog(id, dialog);
}
private void setDialogText(View v) {
if (v instanceof ViewGroup) {
ViewGroup parent = (ViewGroup) v;
int count = parent.getChildCount();
for (int i = 0; i < count; i++) {
View child = parent.getChildAt(i);
setDialogText(child);
}
} else if (v instanceof TextView) {
((TextView) v).setTextSize(40);
}
}
(二)对代码进行简要分析一下:
1. 先讲一下怎么在程序中显示一个progressDialog,需要四步操作(其中第四步为对dialog进行处理,如更改字体字号等,不写也是看以显示系统默认dialog的)。
■声明一个dialogID:
private static final int DLG_SHOW = 0;
■ 创建progressDialog:
@Override
protected Dialog onCreateDialog(int id) {
if (id == DLG_SHOW) {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("loading");
dialog.setCancelable(true);
return dialog;
}
return super.onCreateDialog(id);
}
■显示progressDialog:
showDialog(DLG_SHOW);
■准备progressDialog:
protected void onPrepareDialog(int id, Dialog dialog) {
View v = dialog.getWindow().getDecorView();
setDialogText(v);
super.onPrepareDialog(id, dialog);
}
2. 以上是讲怎么在activity中显示一个progressDislog,比较简单。现在重点讲解一下怎么更改dialog字体,主要分为2步。
■ 先取到dialog整个view:
View v = dialog.getWindow().getDecorView();//取到dialog的整个view
setDialogText(v);//并对该view进行设置
■ 然后遍历v,找到所有textview,设置大小。我写了个方法:
private void setDialogText(View v) {
if (v instanceof ViewGroup) {
ViewGroup parent = (ViewGroup) v;
int count = parent.getChildCount();
for (int i = 0; i < count; i++) {
View child = parent.getChildAt(i);
setDialogText(child);
}
} else if (v instanceof TextView) {
((TextView) v).setTextSize(40); //是textview,设置字号
}
}