2016-09-14 26 views
12

Użyłem AlertDialog, aby ostrzec użytkownika o konieczności usunięcia. Przynajmniej w moim urządzeniu (Android 5.1) i pokazać dobrzeOkno dialogowe nie pokazuje przycisku dodatniego i ujemnego.

enter image description here

Ale na niektóre inne urządzenie (również uruchomić Android 5.1), okno brakowało przycisk dodatnie i ujemne.

enter image description here

Sprawdziłam i okazało się, że urządzenia zdarzyć ten problem mają średniej rozdzielczości (960x540, 854x480).

Czy rozdzielczość dotyczy tego problemu? Jeśli nie, czy możesz podać przyczynę i sposób rozwiązania tego problemu?

Mój kod do okna wyświetlacza:

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

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

Odpowiedz

0

Jest naprawdę odnoszą się do uchwały, nie wiem dokładnie przyczynę i po prostu zrobić jeśli else warunkiem rozwiązania tego problemu.

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

Nadzieję pomóc dla innych deweloperów, którzy mają ten sam problem.

+0

Co to wszystko znaczy? gdzie jest PreferenceUtil? gdzie są style "todoDialogLight" i "todoDialogDark"? proszę poprawić swoją odpowiedź – usman

22

Więc przyciski są dla mnie. Niestety, były one białym tekstem na białym tle. Nie ma to nic wspólnego z rozdzielczością, ale ma więcej wspólnego z tematem, który wybierasz. Aby rozwiązać ten problem, musisz ustawić odpowiedni kolor tekstu w oknie dialogowym.

Na przykład w styles.xml dodać

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

aw swojej działalności dodać

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

nadzieję, że to pomaga.

+2

przejście na "colorAccent" pracy dla mnie. – Ancee

2

Rozwiązanie Ali działało dla mnie. Mój oryginalny kod działał na poprzednich wersjach Androida < 7. Ale testowanie mojego Pixela dało niewidoczne przyciski. Dodałem koncepcję stylu opisaną przez Ali, jak pokazano poniżej i wszystko jest dobrze:

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

Okno dialogowe dialogowe;

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 wypróbuj ten kod. Ten kod zadziałał dla mnie –

0

Jeśli używasz niestandardowego motywu w swojej styles.xml ustawić kolor colorAccent do ciemniejszego koloru.

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

Dodaj to w dobrym stylu.xml:

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

i użytkowania działalności

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