Android Dialog 设置字体大小 Android Dialog 设置字体大小的具体方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android Dialog 设置字体大小 Android Dialog 设置字体大小的具体方法

  2021-03-20 我要评论
想了解Android Dialog 设置字体大小的具体方法的相关内容吗,在本文为您仔细讲解Android Dialog 设置字体大小的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,Dialog,字体大小,下面大家一起来学习吧。

先看下面图片:

这是我在做登录页面的时候,调用系统的ProgressDialog 进行等待,可是看起来很不协调,左边的等待图片过大,右边文字过小,看起来老别扭,虽然功能上不存在什么问题,但是我有强迫症,看不顺的就像弄掉。可是找了好久,没发现 ProgressDialog  有一个方法是可以设置字体的。

于是我又来CSDN查找解决方案,可是找了好久,翻了好几页都没看到想要的结果,心冷了,找到的都说ProgressDialog 可以自定义一个View,在layout定义一个布局,然后设置到ProgressDialog 中,这确实是一个解决办法,可是对我来说颇显麻烦,我只是要一个等待效果,改一下字体,费不着去写一个layout,在重写一个ProgressDialog 吧。

最后我想想,可以设置ProgressDialog  的layout 那么应该也可以获取他的View吧,果然Dialog 就有一个获取View的方法:

复制代码 代码如下:

public abstract View getDecorView ()
Added in API level 1
Retrieve the top-level window decor view (containing the standard window frame/decorations and the client's content inside of that), which can be added as a window to the window manager.

Note that calling this function for the first time "locks in" various window characteristics as described in

只要有了View 我就可以找到其中的TextView,并设置相应的字体大小,一下是我的实现代码:

复制代码 代码如下:

    /**
     * 显示 进度对话框
     * @param message 消息
     * @param cancel 是否可取消
     * @param textsize 字体大小
     */
    protected final void showProgressDialog(String message,boolean cancel,int textsize)
    {
        // TODO Auto-generated method stub
        mProgress = new ProgressDialog(this);
        mProgress.setMessage(message);
        mProgress.setCancelable(cancel);
        mProgress.setOnCancelListener(null);
        mProgress.show();

        setDialogFontSize(mProgress,textsize);
    }
    private void setDialogFontSize(Dialog dialog,int size)
    {
        Window window = dialog.getWindow();
        View view = window.getDecorView();
        setViewFontSize(view,size);
    }
    private void setViewFontSize(View view,int size)
    {
        if(view instanceof ViewGroup)
        {
            ViewGroup parent = (ViewGroup)view;
            int count = parent.getChildCount();
            for (int i = 0; i < count; i++)
            {
                setViewFontSize(parent.getChildAt(i),size);
            }
        }
        else if(view instanceof TextView){
            TextView textview = (TextView)view;
            textview.setTextSize(size);
        }
    }

最后看效果图:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们