gpt4 book ai didi

android - 更改 AlertDialog.Builder 按钮颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:38 25 4
gpt4 key购买 nike

我试图更改 AlertDialog.Builder 的按钮颜色,但我没有找到方法。

我想将按钮的颜色和标题更改为白色,就像在 HOLO 主题中一样。

请参阅以下 2 个屏幕截图以获取示例:

enter image description here

enter image description here

我看过这里:

How to change theme for AlertDialog

Change the style of AlertDialog

How to change the background of the custom alert dialog

Applying Styles Android

所有这些都不适合我。

这是我的代码:

public void logInDialog()
{
ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.dialogStyle);
AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
builder.setTitle("Log in");
View prefView = View.inflate(this, R.layout.log_in, null);
//The rest of the code.........
}

这是我的样式代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="dialogStyle" parent="android:Theme.Dialog">
<item name="android:background">@color/white</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@color/white</item>
</style>
</resources>

最佳答案

我知道这是一个非常古老的问题,但我遇到了同样的问题并且找到了解决方案。为了更改警报对话框按钮内文本的颜色,您应该这样做:

public void logInDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = context.getLayoutInflater();

//setting custom view for our dialog
View myview = inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT, null);
builder.setNeutralButton(android.R.string.cancel, null);
builder.setView(myview);

//creating an alert dialog from our builder.
AlertDialog dialog = builder.create();
dialog.show();

//retrieving the button view in order to handle it.
Button neutral_button = dialog.getButton(DialogInterface.BUTTON_NEUTRAL);

Button positive_button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);


if (neutral_button != null) {
neutral_button.setBackgroundDrawable(context.getResources()
.getDrawable(R.drawable.custom_background));

neutral_button.setTextColor(context.getResources()
.getColor(android.R.color.white));
}
if (positive_button != null) {
positive_button.setBackgroundDrawable(context.getResources()
.getDrawable(R.drawable.custom_background));

positive_button.setTextColor(context.getResources()
.getColor(android.R.color.white));
}

}

以及您使用的按钮的 xml:

自定义背景.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:drawable="#000000"/>
<item android:drawable="@drawable/selectable_item_background"/>

</layer-list>

和 selectable_item_background.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/item_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/item_focused" android:state_focused="true"/>
<item android:drawable="@drawable/item_focused" android:state_selected="true"/>
<item android:drawable="@android:color/transparent"/>

</selector>

我个人在 Fragment 中使用了这段代码,这就是我使用 LayoutInflater 的原因。在您的情况下,您可以跳过此步骤。希望它能在未来帮助其他人。

关于android - 更改 AlertDialog.Builder 按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17901072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com