0

在 AlertDialog 中,我可以设置 Message 的大小,但是当我对 Title 执行相同操作时,它会崩溃。

代码:

AlertDialog dialog = new AlertDialog.Builder(this).setMessage(message).setTitle(title)
               .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                      dialog.cancel();
                   }
               }).show();
     TextView titleTxt= (TextView)dialog.findViewById(android.R.id.title);
     **titleTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);** // crash here 
     TextView textView = (TextView) dialog.findViewById(android.R.id.message);
     **textView.setTextSize(40);** // works fine



 Button btn1 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
   btn1.setTextSize(36); 

我的输出:

05-10 11:49:56.917: E/AndroidRuntime(8376): FATAL EXCEPTION: main
05-10 11:49:56.917: E/AndroidRuntime(8376): java.lang.NullPointerException
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.binary.taxitop.LoginScreen.showAlertDialog(LoginScreen.java:116)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.binary.taxitop.LoginScreen.VerifyCredentialsAndEnterNextScreen(LoginScreen.java:103)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.binary.taxitop.LoginScreen.onClick(LoginScreen.java:84)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.view.View.performClick(View.java:3511)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.view.View$PerformClick.run(View.java:14109)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.os.Handler.handleCallback(Handler.java:605)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.os.Looper.loop(Looper.java:137)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at java.lang.reflect.Method.invoke(Method.java:511)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at dalvik.system.NativeStart.main(Native Method)

提前致谢。

4

3 回答 3

5

只需更改这一行:

TextView titleTxt= (TextView)dialog.findViewById(getApplicationContext().getResources().getIdentifier( "alertTitle", "id", "android" ));
于 2013-05-10T09:31:06.590 回答
0

here is my code when I encountered situation like you.

Edit:-

    String title = "Title";
    SpannableStringBuilder Builder = new SpannableStringBuilder(title);//Your title goes here
     StyleSpan spanState1 = new StyleSpan(Typeface.ITALIC);//You can make your title italic ,bold etc here
     //ScaleXSpan spanState2 = new ScaleXSpan(4);// As far your font is concern, this will help

Builder.setSpan(new RelativeSizeSpan(2.8f),  0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);// set your size there                     
     Builder.setSpan(spanState1, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
     //Builder.setSpan(spanState2, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

      AlertDialog.Builder dialog_build = new AlertDialog.Builder(MainActivity.this);

      dialog_build.setTitle(Builder);

      dialog_build.show();

Focused on title only, because that was your concern..

Hope, this helps

于 2013-05-10T07:10:49.767 回答
0

据我所知,没有直接的方法来修改titleinAlertDialog

您可以创建自定义对话框或使用反射

下面的反射代码来自网络,希​​望它有用

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();  
}  
于 2013-05-10T07:00:52.583 回答