服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android Dialog 设置字体大小的具体方法

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

2021-02-05 14:58Android开发网 Android

这篇文章介绍了Android Dialog 设置字体大小的具体方法,希望能帮助到有同样需求的朋友,可能我的方法不是最好的,也希望有朋友指点

先看下面图片:

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

 

最后看效果图:

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

延伸 · 阅读

精彩推荐