2016-09-14 115 views
12

我用AlertDialog提醒用户确认删除。我检查我的设备(Android的5.1),并将其呈现非常对话框不显示正负按钮

enter image description here

但在某些其他设备(也运行Android 5.1),对话框错过了正面和负面的按钮。

enter image description here

我查了一下,发现设备出现这个问题有一个中等分辨率(960×540,854×480)。

分辨率是否与此问题有关? 如果不是,你能告诉我原因以及如何解决这个问题吗?

我显示的对话框代码:

public static final Dialog yesNoDialog(Context context, 
               String message, 
               DialogInterface.OnClickListener yesAction, DialogInterface.OnClickListener noAction) { 


      AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.todoDialogLight); 

      builder.setTitle(context.getString(R.string.app_name)) 
        .setMessage(message) 
        .setCancelable(false) 
        .setPositiveButton("YES", yesAction) 
        .setNegativeButton("NO", noAction); 
      return builder.create(); 
} 

而styles.xml

<style name="todoDialogLight" parent="Theme.AppCompat.Light.Dialog"> 

      <!-- Used for the buttons --> 
      <item name="colorAccent">@color/colorPrimaryDark</item> 
      <item name="android:textStyle">bold</item> 
      <!-- Used for the title and text --> 
      <item name="android:textColorPrimary">@color/colorText</item> 
      <!-- Used for the background --> 
      <!-- <item name="android:background">#4CAF50</item>--> 
      <item name="android:fontFamily">sans-serif</item> 
      <item  name="android:windowAnimationStyle">@style/RemindDialogAnimation</item> 
      <item name="android:layout_width">@dimen/width_remind_dialog</item> 
      <item name="android:layout_height">wrap_content</item> 
</style> 

回答

0

它确实涉及到解决,我不知道确切的原因,如果其他人只是做一个条件来解决这个问题。

public static String getDensity(Context context) { 
     float density = context.getResources().getDisplayMetrics().density; 
     if (density >= 4.0) { 
      return "xxxhdpi"; 
     } 
     if (density >= 3.0) { 
      return "xxhdpi"; 
     } 
     if (density >= 2.0) { 
      return "xhdpi"; 
     } 
     if (density >= 1.5) { 
      return "hdpi"; 
     } 
     if (density >= 1.0) { 
      return "mdpi"; 
     } 
     return "ldpi"; 
} 

AlertDialog

public static Dialog yesNoDialog(final Context context, 
               final String message, 
               final DialogInterface.OnClickListener yesAction, 
               final DialogInterface.OnClickListener noAction) { 
      int theme = PreferenceUtil.getThemeSetting(context, PreferenceUtil.PREF_THEME); 
      AlertDialog.Builder builder = null; 
      String density = AppUtil.getDensity(context); 
      if (theme == ThemeUtil.THEME_LIGHT) { 
       if(density.equals("hdpi")){ 
        builder = new AlertDialog.Builder(context); 
       }else{ 
        builder = new AlertDialog.Builder(context, R.style.todoDialogLight); 
       } 
      } else { 
       if(density.equals("hdpi")){ 
        builder = new AlertDialog.Builder(context); 
       }else{ 
        builder = new AlertDialog.Builder(context, R.style.todoDialogDark); 
       } 
      } 
      builder.setTitle(context.getString(R.string.app_name)) 
        .setMessage(message) 
        .setCancelable(false) 
        .setPositiveButton("YES", yesAction) 
        .setNegativeButton("NO", noAction); 
      return builder.create(); 
    } 

希望它能帮助别人开发谁有同样的问题。

+0

这是怎么一回事? PreferenceUtil在哪里?样式“todoDialogLight”和“todoDialogDark”在哪里?请改进您的回答 – usman

22

所以按钮就在我身边。不幸的是,他们是白色背景上的白色文字。它与决议没有任何关系,但更多与您选择的主题有关。要解决这个问题,您需要在对话框主题中设置正确的文字颜色。

例如,在styles.xml添加

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> 
<item name="colorPrimary">@color/colorPrimaryDarkBlue</item> 
</style> 

,并在你的活动添加

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MyActivity.this, R.style.MyDialogTheme); 

希望这有助于。

+2

更改为“colorAccent”为我工作。 – Ancee

2

阿里的解决方案为我工作。我原来的代码工作在以前的Android版本< 7.但在我的Pixel测试给了不可见的按钮。我添加了如下所示的由阿里详述的样式概念,并且一切都很好:

return new AlertDialog.Builder(getActivity(),R.style.MyDialogTheme) 
      .setView(v) 
      .setTitle(R.string.filter_picker_title) 
      .setPositiveButton(android.R.string.ok, 
        // when the user presses the button to select a new number 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 

          Integer markerIndex = mNumberPicker.getValue(); 
          stringFilter=uniqueValues[markerIndex]; 
          sendResult(Activity.RESULT_OK, stringFilter); 
         } 
        }) 
      .create(); 
0

对话框对话框;

public void startAlertDialog(String message) 
{ 
    AlertDialog.Builder alertDialog=new AlertDialog.Builder(this); 


    alertDialog.setCancelable(false); 

    LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
    View view=inflater.inflate(R.layout.alertdialoglayout,null); 
    TextViewRagular textViewRagular=(TextViewRagular)view.findViewById(R.id.textviewMessage); 
    textViewRagular.setText(message); 
    alertDialog.setView(view); 
    dialog=alertDialog.create(); 
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    dialog.show(); 
} 
+0

Plz试试这段代码。那段代码适合我 –

0

如果您正在使用您的styles.xml设置的colorAccent颜色较暗的颜色自定义主题。

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimary</item> 
     <item name="colorAccent">@color/colorPrimary</item> 
    </style> 
1

添加此样式。XML:

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> 
     <item name="android:textColor">@color/colorAccent</item> 
</style> 

并在活动中使用

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MyActivity.this, R.style.MyDialogTheme);