怎么改变progressDialog消息字体字号

http://blog.csdn.net/fhy_2008/article/details/6318498

今天在论坛看到有人问怎么更改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,设置字号
                }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值