das*_*asd 5 android android-dialog
我试图改变AlertDialog.Builder的按钮颜色,但我没有找到一种方法来做到这一点.
我希望将按钮的颜色和标题更改为白色,就像在HOLO主题中一样.
请参阅以下2个屏幕截图作为示例:


我看过这里:
所有这些都不适合我.
这是我的代码:
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.........
}
Run Code Online (Sandbox Code Playgroud)
这是我的样式代码:
<?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>
Run Code Online (Sandbox Code Playgroud)
lou*_*ros 19
我知道这是一个非常古老的问题,但我遇到了同样的问题,我找到了解决方案.要更改"警报"对话框按钮内文本的颜色,您应该执行以下操作:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
使用按钮的xmls:
custom_background.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>
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
我个人在Fragment中使用了这个代码,这就是为什么我有一个LayoutInflater.在您的情况下,您可以跳过此步骤.希望它能在未来帮助其他人.