先看下面圖片:
這是我在做登錄頁面的時候,調用系統的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);
}
}
最后看效果圖: