Отсутствующие кнопки в AlertDialog | Android 7.0 (Nexus 5x)
Я пытаюсь создать AlertDialog
, но кнопки не отображаются. Только просмотр этой проблемы в Android 7.0:
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onDismiss(final DialogInterface dialog) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.show();
![AlertDialog]()
Ответы
Ответ 1
Действительно, кажется, что необходимо определить тему AlertDialog. Альтернативным подходом к выше будет определение темы AlertDialog в теме приложения:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- ... other AppTheme items ... -->
<item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
</style>
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
Тогда достаточно создать AlertDialog.Builder
только с параметром Context
.
Примечание. Выше, похоже, работает только для android.app.AlertDialog.Builder
и не работает для компоновщика AppCompat (android.support.v7.app.AlertDialog.Builder
, по крайней мере, начиная с версии 25.0.1). В случае компоновщика AppCompat мне пришлось передать идентификатор темы в качестве второго параметра в конструктор Builder, чтобы иметь видимые кнопки.
Ответ 2
Итак, на Android 7.0 вам нужно предоставить тему. По крайней мере, это то, что я должен был сделать.
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="borderlessButtonStyle">@style/Widget.AppCompat.Button.Borderless.Colored</item>
</style>
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme);
Ответ 3
То, что сработало для меня, было в styles.xml:
<style name="LightDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">@android:color/primary_text_light</item>
<item name="colorAccent">#007fff</item>
<item name="buttonBarButtonStyle">@style/DialogButtonStyle</item>
</style>
и
<style name="DialogButtonStyle" parent="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">#007fff</item>
</style>
и в вашей программе:
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme);
Ответ 4
Вы можете создать настраиваемую тему для Alert Dialog и установить alertDialogTheme
в теме приложения.
<!--Alert Dialog Theme -->
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColor">@color/colorPrimary</item>
<item name="buttonBarButtonStyle">@style/DialogButtonStyle</item>
<item name="colorAccent">@color/colorAccent</item>
<!--If minimum API level is greater than or equal to 23, you can define the color of Title text separately -->
<item name="android:titleTextColor">@SomeColor</item>
</style>
<!--This is to style the buttons of alert dialog-->
<style name="DialogButtonStyle" parent="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
<item name="android:textColor">@color/colorAccent</item>
</style>
и, наконец, установите для созданной темы тему alertDialogTheme
в теме приложения:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--To make the change global to application-->
<item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>
Протестировано для android.support.v7.app.AlertDialog
Ответ 5
Вам нужна тема, например:
Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder (Activity, Android.Resource.Style.ThemeMaterialDialogAlert);
Ответ 6
У меня была аналогичная проблема, и дело в том, что я не использовал библиотеку поддержки для своей AppCompatActivity, поэтому я изменил:
import android.app.AlertDialog;
к
import android.support.v7.app.AlertDialog;
и он работал.
Ответ 7
Вы можете добавить пользовательский цвет к кнопке.
Ниже вашего кода
builder.show();
Запишите это
Button bg = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
bg.setTextColor(Color.BLUE);
Ответ 8
Возможно, слишком поздно, но я надеюсь, что кто-то воспользуется этим решением.
вы можете сделать это примерно так: вы должны установить onShowListenter в свой alertDialog, внутри этой функции вы должны getButton() и setTextColor к ней. Пример:
alertDialog = alertDialogBuilder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
@Override
public void onShow(DialogInterface dialogInterface){
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(R.color.black);
}
});