AlertDialog 텍스트 내용 수정 색상

최근 연상 A858T 흰색 휴대전화에서 다음과 같은 AlertDialog를 테스트했을 때 AlertDialog 배경은 기본적으로 흰색이고 타이틀, 메시지는 검은색이지만 CheckBox의 Text는 흰색이었다.
final CheckBox cb = new CheckBox(this);
            cb.setChecked(false);
            cb.setText(getResources().getString(R.string.close_wifi_switch));
            dialog = new AlertDialog.Builder(this)
                    .setTitle(getResources().getString(R.string.exit_wimo_sure))
                    .setView(cb)
                    .setNegativeButton(R.string.cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    // Do nothing.
                                }
                            })
                    .setPositiveButton(R.string.exit,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                   
                                }
                            }).create();
            dialog.show();

그러므로 반사법으로 AlertDialog의 title 색상 값을 읽고 이를 CheckBox의 Text Color에 부여할 수 있는지 생각한 후에 인터넷에서 관련 Alert Controller 클래스를 찾았다. 이 클래스는 AlertDialog의 실현 클래스로 외부에 공개되지 않았다. 그리고 이 클래스에 개인 구성원 변수가 mTitle View라고 있는데 이것이 바로 AlertDialog의 title Text View이다. 그래서 이 구성원 변수의 실례를 얻으면타이틀의 색상 값을 얻을 수 있습니다
 dialog.show();//    ,               
        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);
            ColorStateList colorStateList = title.getTextColors();
            cb.setTextColor(colorStateList);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

checkBox의 Text 색상을 수정하는 것과 유사하며, AlertDialog의 title 글꼴 색상, title를 직접 수정할 수도 있습니다.setTextColor(XXXXX)

좋은 웹페이지 즐겨찾기