AlertDialog上缺少按钮| Android 7.0(Nexus 5x)

时间:2023-01-17 15:17:19

I am trying to create an AlertDialog but the buttons are not showing. Only seeing this issue in Android 7.0:

我正在尝试创建一个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上缺少按钮| Android 7.0(Nexus 5x)

9 个解决方案

#1


30  

Indeed it seems that AlertDialog theme needs to be defined. An alternative approach to above would be to define AlertDialog theme in Application theme:

事实上,似乎需要定义AlertDialog主题。上面的另一种方法是在Application主题中定义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>

Then it is enough create AlertDialog.Builder only with Context parameter.

然后只使用Context参数创建AlertDialog.Builder就足够了。

Note: The above seems to work only for android.app.AlertDialog.Builder and is not working for AppCompat builder (android.support.v7.app.AlertDialog.Builder, at least as of version 25.0.1). In case of AppCompat builder, I had to pass theme ID as second parameter to Builder constructor to have buttons visible.

注意:以上似乎仅适用于android.app.AlertDialog.Builder,并且不适用于AppCompat构建器(android.support.v7.app.AlertDialog.Builder,至少从版本25.0.1开始)。在AppCompat构建器的情况下,我必须将主题ID作为第二个参数传递给Builder构造函数以使按钮可见。

#2


12  

So it turns out on Android 7.0 you have to provide a theme. At least, that's what I had to do.

所以在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


8  

What worked for me was in styles.xml:

对我有用的是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>

and

<style name="DialogButtonStyle" parent="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">#007fff</item>                   
</style>

and in your program:

在你的程序中:

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme);

#4


4  

You can create a custom theme for Alert Dialog, and set alertDialogTheme in your app theme.

您可以为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>

and finally, set the custom created theme to alertDialogTheme in Application Theme:

最后,在Application Theme中将自定义创建的主题设置为alertDialogTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--To make the change global to application-->
    <item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>

Tested for android.support.v7.app.AlertDialog

测试android.support.v7.app.AlertDialog

#5


0  

You need use a theme, like this:

你需要使用一个主题,如下所示:

Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder (Activity, Android.Resource.Style.ThemeMaterialDialogAlert);

#6


0  

I had a similar issue and the thing was that I wasn't using the support library for my AppCompatActivity, therefore I changed:

我有一个类似的问题,事情是我没有使用支持库来进行AppCompatActivity,因此我改变了:

import android.app.AlertDialog;

to

import android.support.v7.app.AlertDialog;

and it worked.

它工作。

#7


0  

You can add custom colour to button. Below your code

您可以为按钮添加自定义颜色。在你的代码下面

builder.show();

Write this

Button bg = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
bg.setTextColor(Color.BLUE);

#8


-1  

Sorry for late answer, Its about import problem. you need to select v7.alertDialog, not use app.alertDialog.

对不起,迟到的答案,关于进口问题。你需要选择v7.alertDialog,而不是使用app.alertDialog。

Please change your code to v7.alertDialog and you will show button color in nexus as well.

请将您的代码更改为v7.alertDialog,您还将在nexus中显示按钮颜色。

AlertDialog上缺少按钮| Android 7.0(Nexus 5x)

#9


-2  

Maybe its too late, but I hope someone would use this solution. you can do it something like this: You should set onShowListenter to your alertDialog, inside this function you should getButton() and than setTextColor to it. An example:

也许为时已晚,但我希望有人会使用这个解决方案。你可以这样做:你应该将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);
    }
});

#1


30  

Indeed it seems that AlertDialog theme needs to be defined. An alternative approach to above would be to define AlertDialog theme in Application theme:

事实上,似乎需要定义AlertDialog主题。上面的另一种方法是在Application主题中定义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>

Then it is enough create AlertDialog.Builder only with Context parameter.

然后只使用Context参数创建AlertDialog.Builder就足够了。

Note: The above seems to work only for android.app.AlertDialog.Builder and is not working for AppCompat builder (android.support.v7.app.AlertDialog.Builder, at least as of version 25.0.1). In case of AppCompat builder, I had to pass theme ID as second parameter to Builder constructor to have buttons visible.

注意:以上似乎仅适用于android.app.AlertDialog.Builder,并且不适用于AppCompat构建器(android.support.v7.app.AlertDialog.Builder,至少从版本25.0.1开始)。在AppCompat构建器的情况下,我必须将主题ID作为第二个参数传递给Builder构造函数以使按钮可见。

#2


12  

So it turns out on Android 7.0 you have to provide a theme. At least, that's what I had to do.

所以在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


8  

What worked for me was in styles.xml:

对我有用的是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>

and

<style name="DialogButtonStyle" parent="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">#007fff</item>                   
</style>

and in your program:

在你的程序中:

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme);

#4


4  

You can create a custom theme for Alert Dialog, and set alertDialogTheme in your app theme.

您可以为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>

and finally, set the custom created theme to alertDialogTheme in Application Theme:

最后,在Application Theme中将自定义创建的主题设置为alertDialogTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--To make the change global to application-->
    <item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>

Tested for android.support.v7.app.AlertDialog

测试android.support.v7.app.AlertDialog

#5


0  

You need use a theme, like this:

你需要使用一个主题,如下所示:

Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder (Activity, Android.Resource.Style.ThemeMaterialDialogAlert);

#6


0  

I had a similar issue and the thing was that I wasn't using the support library for my AppCompatActivity, therefore I changed:

我有一个类似的问题,事情是我没有使用支持库来进行AppCompatActivity,因此我改变了:

import android.app.AlertDialog;

to

import android.support.v7.app.AlertDialog;

and it worked.

它工作。

#7


0  

You can add custom colour to button. Below your code

您可以为按钮添加自定义颜色。在你的代码下面

builder.show();

Write this

Button bg = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
bg.setTextColor(Color.BLUE);

#8


-1  

Sorry for late answer, Its about import problem. you need to select v7.alertDialog, not use app.alertDialog.

对不起,迟到的答案,关于进口问题。你需要选择v7.alertDialog,而不是使用app.alertDialog。

Please change your code to v7.alertDialog and you will show button color in nexus as well.

请将您的代码更改为v7.alertDialog,您还将在nexus中显示按钮颜色。

AlertDialog上缺少按钮| Android 7.0(Nexus 5x)

#9


-2  

Maybe its too late, but I hope someone would use this solution. you can do it something like this: You should set onShowListenter to your alertDialog, inside this function you should getButton() and than setTextColor to it. An example:

也许为时已晚,但我希望有人会使用这个解决方案。你可以这样做:你应该将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);
    }
});