简体   繁体   中英

Android DialogFragment text color is white instead of black

I'm creating a simple DialogFragment with a custom adapter but strangely the text appears white when the dialog shows up.

class MyDialog extends DialogFragment {

        ListView listView;

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            ArrayAdapter<Service> adapter = new MyAdaper(getContext(), items);
            TextView title = new TextView(getContext());
            title.setText("Hello");
            title.setGravity(Gravity.CENTER);
            title.setTextSize(18);
            title.setTextColor(Color.BLACK);
            builder.setCustomTitle(title);
            builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            return builder.create();
        }

The layout xml inflated in the adapter is very simple and only has one TextView (I didn't specify any color but I would expect it to be black as the rest of my application when I use a TextView without specifying colors)

I have tried using

AlertDialog.Builder(getActivity(), R.style.MyTheme);

But even if I specify b black textColor in MyThem the Dialog still shows up with white text

What Am I missing ?

Look you can create style for your dialog. For example:

  <style name="cust_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleStyle">@style/dialog_title_style</item>
  </style>

  <style name="dialog_title_style" parent="android:Widget.TextView">
    <item name="android:background">@android:color/holo_red_dark</item>
  </style>

Than set up it into onCreate

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.cust_dialog);
  }

<item name="android:textColorPrimary">@color/your desired color</item>

警报对话框样式的styles.xml 文件中的这行代码将有助于更改警报对话框中消息正文的字体颜色。

Try builder.setMessage(R.id.<yours id-name of TextView>); But must have define the color of TextView in your XML code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM