Android AlertDialog更改標題顏色,字體等


更改AlertDialog標題的方法google目前沒有提供,只能通過其他辦法

一種辦法是:首先在源代碼中找到有個叫AlertController的類,這個類就是AlertDialog的實現類,是沒有對外公開的,然后在這個類中有個私有成員變量叫mTitleView,這個就是AlertDialog的title的TextView,所以只要得到這個成員變量的實例,即可自定義AlertDialog的title

得到這個的實例變量的方法通過兩步反射來實現,如下:

    AlertDialog dialog = (AlertDialog) getDialog();
    try {
        Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
        mAlert.setAccessible(true);
        Object alertController = mAlert.get(dialog);
 
        Field mTitleView = alertController.getClass().getDeclaredField("mTitleView");
        mTitleView.setAccessible(true);
 
        TextView title = (TextView) mTitleView.get(alertController);
        title.setTextColor(0xff33b5e5); 
 
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

當然還有其他辦法,比如直接把title隱藏掉,然后在content View中自定義一個title出來等等


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM