ProgressDialog修改TextView的TextSize

ProgressDialog修改TextView的TextSize

问题描述

今天UI过来找我说是加载条的字号太小了,不好看,希望可以改一下,然后我就研究一下如何修改ProgressDialog里面TextView的字体大小。

问题分析

加载条是用ProgressDialog控件来实现的,然后找了一下,发现没有提供给方法可以直接修改TextView的TextSize,所以我就上网搜索了,哈哈哈,不会就查百度,果然百度有解决方法,看了一下,解决方法可以归为两种。
第一种方法:修改style

    <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textSize">25sp</item>
    </style>

先使用了一下这种方法,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);
    }
}

测试了一下,这种方法是有用的,也没有副作用,最终决定使用这种方法。

实现解决

因为代码中有很多地方使用的ProgressDialog,如果一个地方一个地方去加代码,我还是不太愿意的,只要是因为太懒了,所以我就写了一个简单的View继承ProgressDialog,然后在类中寻找在哪里添加这个修改字体大小的代码,发现了onCreate()方法,深得我心,就加在了onCreate()方法中,类代码如下:

package cn.dream.ebagcomlib.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 可修改TextView字体大小的ProgressBar
 * Author: zhangmiao
 * Date: 2018/4/13
 */
public class ModifyTextSizeProgressDialog extends ProgressDialog {

    private static final String TAG = ModifyTextSizeProgressDialog.class.getSimpleName();

    private float mTextSize;

    public ModifyTextSizeProgressDialog(Context context) {
        super(context);
        mTextSize = 18;
    }

    public ModifyTextSizeProgressDialog(Context context, float textSize) {
        super(context);
        mTextSize = textSize;
    }

    public ModifyTextSizeProgressDialog(Context context, int theme) {
        super(context, theme);
        mTextSize = 18;
    }

    public ModifyTextSizeProgressDialog(Context context, int theme, float textSize) {
        super(context, theme);
        mTextSize = textSize;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setDialogTextSize(getWindow().getDecorView());
    }

    private void setDialogTextSize(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);
                setDialogTextSize(child);
            }
        } else if (v instanceof TextView) {
            ((TextView) v).setTextSize(mTextSize);
        }
    }
}

然后就解决问题了。

日常挣扎

但是以为就完了,当然不是,还是要日常挣扎一下的,我在想我都发现了onCreate()方法了,我就不能像Activity一样在里面使用findViewById()方法找到我需要修改的TextView控件,然后直接setTextSize一下,还写的少,接着就是查看一下源码,找一下TextView的id了。
ProgressDialog的onCreate()代码:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        TypedArray a = mContext.obtainStyledAttributes(null,
                com.android.internal.R.styleable.AlertDialog,
                com.android.internal.R.attr.alertDialogStyle, 0);
        if (mProgressStyle == STYLE_HORIZONTAL) {

            /* Use a separate handler to update the text views as they
             * must be updated on the same thread that created them.
             */
            mViewUpdateHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);

                    /* Update the number and percent */
                    int progress = mProgress.getProgress();
                    int max = mProgress.getMax();
                    if (mProgressNumberFormat != null) {
                        String format = mProgressNumberFormat;
                        mProgressNumber.setText(String.format(format, progress, max));
                    } else {
                        mProgressNumber.setText("");
                    }
                    if (mProgressPercentFormat != null) {
                        double percent = (double) progress / (double) max;
                        SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
                        tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
                                0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        mProgressPercent.setText(tmp);
                    } else {
                        mProgressPercent.setText("");
                    }
                }
            };
            View view = inflater.inflate(a.getResourceId(
                    com.android.internal.R.styleable.AlertDialog_horizontalProgressLayout,
                    R.layout.alert_dialog_progress), null);
            mProgress = (ProgressBar) view.findViewById(R.id.progress);
            mProgressNumber = (TextView) view.findViewById(R.id.progress_number);
            mProgressPercent = (TextView) view.findViewById(R.id.progress_percent);
            setView(view);
        } else {
            View view = inflater.inflate(a.getResourceId(
                    com.android.internal.R.styleable.AlertDialog_progressLayout,
                    R.layout.progress_dialog), null);
            mProgress = (ProgressBar) view.findViewById(R.id.progress);
            mMessageView = (TextView) view.findViewById(R.id.message);
            setView(view);
        }
        a.recycle();
        if (mMax > 0) {
            setMax(mMax);
        }
        if (mProgressVal > 0) {
            setProgress(mProgressVal);
        }
        if (mSecondaryProgressVal > 0) {
            setSecondaryProgress(mSecondaryProgressVal);
        }
        if (mIncrementBy > 0) {
            incrementProgressBy(mIncrementBy);
        }
        if (mIncrementSecondaryBy > 0) {
            incrementSecondaryProgressBy(mIncrementSecondaryBy);
        }
        if (mProgressDrawable != null) {
            setProgressDrawable(mProgressDrawable);
        }
        if (mIndeterminateDrawable != null) {
            setIndeterminateDrawable(mIndeterminateDrawable);
        }
        if (mMessage != null) {
            setMessage(mMessage);
        }
        setIndeterminate(mIndeterminate);
        onProgressChanged();
        super.onCreate(savedInstanceState);
    }

再查看一下ProgressDialog的setMessage()方法修改的哪个TextView的内容:

    @Override
    public void setMessage(CharSequence message) {
        if (mProgress != null) {
            if (mProgressStyle == STYLE_HORIZONTAL) {
                super.setMessage(message);
            } else {
                mMessageView.setText(message);
            }
        } else {
            mMessage = message;
        }
    }

追踪一下super.setMessage(message)方法,跳转到AlertDialog的方法:

    public void setMessage(CharSequence message) {
        mAlert.setMessage(message);
    }

然后我就发现我gg了,我的想法还是不能实现的,因为我没有办法去获取mAlert组件去修改它的TextSize。所以就使用前面的方法了。

联系方式:1006299425@qq.com,有问题欢迎大家指出。
原文地址:https://www.cnblogs.com/zhangmiao14/p/8951113.html