我知道这是一个非常古老的问题,但我遇到了同样的问题并找到了解决方案。为了更改警报对话框按钮内的文本颜色,您应该执行以下操作:
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));
}
}
并且您使用的按钮的 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>
和 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 的原因。在您的情况下,您可以跳过此步骤。希望它在未来对其他人有所帮助。