开发者

XML attributes for text color of alert dialog

开发者 https://www.devze.com 2023-03-25 18:36 出处:网络
I\'m applyi开发者_开发百科ng a theme: <style name=\"myAlertTheme\" parent=\"@android:style/AlertDialog\">

I'm applyi开发者_开发百科ng a theme:

  <style name="myAlertTheme" parent="@android:style/AlertDialog">
    <item name="android:textColor">#ff000000</item>
  </style>
  <style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:textColor">#ff000000</item>
  </style>
  <style name="HSDroidStyle" parent="android:Theme">
    <item name="android:background">#ffd3d3d3</item>
    <item name="android:textColor">#ff000000</item>
    <item name="android:textSize">20dp</item>
    <item name="android:radioButtonStyle">@style/RadioButton</item>
</style>

But when I create an Alert, the text shows up as white instead of black. To create the alert I'm using:

    AlertDialog alertDialog = new AlertDialog.Builder(
            new ContextThemeWrapper(act, R.style.myAlertTheme)).create();  
    alertDialog.setTitle(title);  
    alertDialog.setMessage(msg);  
    alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {  
      public void onClick(DialogInterface dialog, int which) {  
          if (exitOnFailure) mainAct.finish();
        return;  
    } });           
    alertDialog.show();

Now the "OK" in the button changes if I change the size in the myAlertTheme, but not the title or the text. So I'm guessing that there is some other textColor attribute that addresses this. I've tried most of the ones I can think of, but none of them work.

Any ideas?


hooked82 is right. Prior to Honeycomb, Android did not support text styling properly for AlertDialogs. You could apply styles to an AlertDialog, but it would ignore the text styles.

Here's a good SO answer that would prove to be a good solution for you: How to change theme for AlertDialog

Here's a really useful blog post that uses the propsed solution above to style an AlertDialog properly.


AFAIK there isn't a way to modify the Title/Message text colors (Please correct me if I'm wrong). So one way of doing this would be to create a custom view for your AlertDialog and do something like the following:

LayoutInflater factory = LayoutInflater.from(this);
final View layout = factory.inflate(R.layout.alert_layout, null);
alertDialog.setView(layout);


Try to declare the AlertDialog using this statement:

AlertDialog alertDialog = new AlertDialog(mContext, R.style.myAlertTheme);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号