ProgressDialog modifies the TextSize of TextView

ProgressDialog modifies the TextSize of TextView

Problem Description

The UI came to me today and said that the font size of the loading bar is too small and not good-looking. I hope it can be changed, and then I will study how to modify the font size of the TextView in the ProgressDialog.

problem analysis

The loading bar is implemented with the ProgressDialog control, and then I looked for it and found that there is no method provided to directly modify the TextSize of the TextView, so I searched the Internet, hahaha, if I don't know, I will check Baidu. Sure enough, Baidu has a solution, see For a moment, the solutions can be classified into two types.
The first method: modify the 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>

I used this method first, the font of the TextView has changed, but another problem is that the loaded frame is so ugly, I can't stand it, so I gave up this method.

The second method: modify in the code

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

After testing it, this method is useful and has no side effects, and finally decided to use this method.

implement solution

Because there are ProgressDialog used in many places in the code, I am still reluctant to add code in one place and one place, as long as it is too lazy, so I wrote a simple View to inherit ProgressDialog, and then look for it in the class Where to add this code to modify the font size, I found the onCreate() method, which won my heart, so I added it to the onCreate() method. The class code is as follows:

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

Then the problem is solved.

daily struggle

But I thought it was over, of course not, I still have to struggle every day, I think I have discovered the onCreate() method, I can't use the findViewById() method in it like an Activity to find the TextView control I need to modify, and then Just setTextSize directly, and write less, then check the source code to find the id of TextView.
ProgressDialog's onCreate() code:

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

Look at the content of which TextView modified by the setMessage() method of ProgressDialog:

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

Follow the super.setMessage(message) method and jump to the AlertDialog method:

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

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

联系方式:[email protected],有问题欢迎大家指出。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324869946&siteId=291194637