2016-09-14 28 views
12

Tôi đã sử dụng AlertDialog để cảnh báo người dùng xác nhận xóa. Tôi kiểm tra điện thoại của tôi (Android 5.1) và nó hiển thị tốtHộp thoại không hiển thị nút tích cực và tiêu cực

enter image description here

Nhưng trên một số thiết bị khác (cũng chạy Android 5.1), hộp thoại nhỡ nút tích cực và tiêu cực.

enter image description here

tôi đã kiểm tra và phát hiện ra rằng các thiết bị xảy ra vấn đề này có độ phân giải trung bình (960x540, 854x480).

Độ phân giải có liên quan đến vấn đề này không? Nếu không, bạn có thể cho tôi biết lý do và cách khắc phục sự cố này không?

Mã của tôi cho hộp thoại hiển thị:

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(); 
} 

Và 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> 

Trả lời

0

Nó thực sự là liên quan đến độ phân giải, tôi không biết chính xác lý do và chỉ cần thực hiện một nếu khác điều kiện để khắc phục sự cố này.

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(); 
    } 

Hy vọng nó sẽ giúp cho nhà phát triển những người khác có cùng một vấn đề.

+0

là gì tất cả điều này? PreferenceUtil ở đâu? các kiểu "todoDialogLight" và "todoDialogDark" ở đâu? xin vui lòng cải thiện câu trả lời của bạn – usman

22

Vì vậy, các nút có sẵn cho tôi. Thật không may, họ là văn bản màu trắng trên nền trắng. Nó không có gì để làm với độ phân giải nhưng nhiều hơn để làm với chủ đề bạn đang lựa chọn. Để giải quyết vấn đề này, bạn cần đặt màu văn bản phù hợp trong chủ đề hộp thoại của mình.

Ví dụ, trong styles.xml thêm

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

và trong hoạt động của bạn thêm

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

Hope this helps.

+2

thay đổi thành "colorAccent" làm việc cho tôi. – Ancee

2

Giải pháp của Ali làm việc cho tôi. Mã gốc của tôi đã hoạt động trên các phiên bản Android trước đó < 7. Nhưng thử nghiệm trên Pixel của tôi đã cung cấp các nút ẩn. Tôi đã thêm khái niệm phong cách chi tiết của Ali như được hiển thị bên dưới và tất cả đều tốt:

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

Hộp thoại thoại;

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 thử mã này. Mã này đã hoạt động đối với tôi –

0

Nếu bạn đang sử dụng một chủ đề tùy chỉnh trong bạn styles.xml thiết lập màu sắc của colorAccent đến một màu tối hơn.

<!-- 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

Thêm kiểu này theo kiểu.xml:

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

và sử dụng hoạt động

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MyActivity.this, R.style.MyDialogTheme); 
Các vấn đề liên quan