2016-09-14 134 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);