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,有问题欢迎大家指出。

ProgressDialog修改TextView的TextSize的更多相关文章

  1. Android 修改 TextView 的全局默认颜色。

    如果你的应用中大多数TextView的颜色是红色, 或者其他颜色, 你是为每一个TextView都设置一次颜色, 还是有其他更好的办法, 这里教你怎么修改TextView的默认颜色. 当然我们Text ...

  2. 使用selector修改TextView中字体的颜色

    selector想必大家都用过了,但是在修改字体的颜色的时候还是要细心. 我们在TextView中设置字体颜色一般使用 android:textColor="@color/red" ...

  3. 在代码中修改TextView的DrawableRight图片

    TextView的xml <TextView android:id="@+id/textciew1" android:layout_width="match_par ...

  4. Android为TV端助力:(转载)修改TextView字体样式

    一.开篇 因为 Android 字体相关的内容还比较多的.有时候其实我们只需要调整一下属性就可以满足设计师的需求,或者是一个退后的方案(毕竟有发版的时间卡住了),有一些效果可以大概满足需求. 那么本文 ...

  5. Android 在代码中修改TextView的DrawableRight等方向上的图片

    在XML文件中可以对TextView进行设置: android:drawableTop="@drawable/XXX" android:drawableBottom="@ ...

  6. 动态修改ViewPagerIndicator CustomTabPageIndicator Tab标签文字颜色

    ViewPagerIndicator 的CustomTabPageIndicator 默认是没有Tab选中修改TextView颜色特效的. 可以通过以下方式实现: 新建viewpager_title_ ...

  7. TextView 的新特性,Autosizing 到底是如何实现的? | 源码分析

    一.前言 Hi,大家好,我是承香墨影! 前两天聊了一下 Autosizing 的使用,反映还不错.毕竟是这种能解决实际问题的新 Api,确实在需要的时候,用起来会很顺手. 简单回顾一下,Autosiz ...

  8. Android 基础一 TextView,Style样式,Activity 传值,选择CheckBox 显示密码

    1.修改TextView字体 mTextView = (TextView) findViewById(R.id.textview1); mTextView.setText("I am her ...

  9. Android:TextView控件

    3.2.1    TextView TextView 可以说是 Android 中最简单的一个控件了,你在前面其实也已经和它打过了一 些打交道.它主要用于在界面上显示一段文本信息,比如你在第一章看到的 ...

随机推荐

  1. SQL Server 异常解决:语句被终止。完成执行语句前已用完最大递归 100。

    问题出现业务场景: 我司有个缺料分析报表,有一个字段是适用机种,需要通过BOM递归读取顶层父物料.这个错就是缺料分析报表执行时报的错: 原因分析定位: 通过网上一些资料,猜测应该是某个递归查询语句,遇 ...

  2. spring boot自定义线程池以及异步处理

    spring boot自定义线程池以及异步处理@Async:什么是线程池?线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线程池线程都是后台线程.每个线程都使 ...

  3. 201771010134杨其菊《面向对象程序设计(java)》第十七周学习总结

    第十七周学习总结 1. 程序是一段静态的代码,它是应用程序执行的蓝本.进程是程序的一次动态执行,它对应了从代码加载.执行至执行完毕的一个完整过程.操作系统为每个进程分配一段独立的内存空间和系统资源,包 ...

  4. paxos made more simple

    paxos算法是进入分布式领域的一块基石,有关paxos的讨论有很多精彩的详细论述,很多牛人不惜宝贵时间以大幅详尽段落叙述.感谢他们,paxos more simple 理解paxos前,我建议以面到 ...

  5. 关于python,完善我计算机知识的一步。

    因为身为理科男,所以特别喜欢涉及其他领域的知识.而对我来说,计算机是很有诱惑力的--尤其是程序语言设计,懂得一门“外语”是多么的重要.大一时候接触过包括有计算机的基本知识,c语言,这个新的学期也开始接 ...

  6. 《Miracle_House》团队项目系统设计改进

    一.团队项目系统设计改进: 1.分析项目系统设计说明书初稿的不足,特别是软件系统结构模型建模不完善内容: 对于原文档中,设计图中存在的错误以及文字描述不准确的地方进行了修改. 2. 团队项目Githu ...

  7. Appium+Python自动化 4 -appium元素定位

    appium定位app上的元素方式 在前面文章中有介绍,(通过UIautomator工具查看app元素) appium定位app上的元素,可以通过id,name,class这些属性定位到 1.id 定 ...

  8. 解决vs验证控件报错” WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping”问题

    将RequiredFieldValidator的 EnableClientScript属性设置成 False 适用于大多验证控件

  9. scrum与第一次teamwork

    一.关于Scrum Scrum是什么?是迭代式增量软件开发过程,通常用于敏捷软件开发,Scrum是一种偏重于过程的敏捷开发的具体方式.Scrum的英文意思是橄榄球运动的一个专业术语,表示“争球”的动作 ...

  10. ehcache缓存使用

    CacheUtils.java //工具类 保存cache缓存: CacheUtils.put(CacheUtils.SIGN_CACHE, childid + "_" + mNu ...