如何更改 AlertDialog 按钮面板(操作按钮后面的区域)的颜色?
How can I change the color of an AlertDialog button panel (the area behind the action buttons)?
我的应用显示 AlertDialog
page1WalkthroughDialogBuilder = new AlertDialog.Builder(activity, R.style.Widget_AppCompat_Light_ActionBar);
page1WalkthroughDialogBuilder
.setCancelable(false)
.setView(page1WalkthroughDialogLayout)
.setPositiveButton(getString(R.string.goto_next), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
page2Walkthrough.show();
dialog.cancel();
}
})
.setNegativeButton(getString(R.string.exit_dialog), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
manager.beginTransaction().show(LaunchActivity.editProfileFragment).commit();
dialog.cancel();
}
});
page1Walkthrough = page1WalkthroughDialogBuilder.create();
使用以下布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rel_layout"
android:layout_height="match_parent"
android:background="@drawable/park_with_people"
mlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
android:text="@string/welcome_hdr"
android:textSize="35sp"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp" />
<uomini.com.wegrokstrasbourg.TextViewWithImages
android:id="@+id/body"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/heading"
android:text="@string/welcome_text"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp" />
</RelativeLayout>
除了按钮有白色背景外,一切都显示得很好:
如何更改背景颜色?
您应该创建一个样式,然后将其应用到 Builder constructor
:
<style name="MyTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/myColor</item>
</style>
然后:
AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyTheme)
...
...
.create();
我的应用显示 AlertDialog
page1WalkthroughDialogBuilder = new AlertDialog.Builder(activity, R.style.Widget_AppCompat_Light_ActionBar);
page1WalkthroughDialogBuilder
.setCancelable(false)
.setView(page1WalkthroughDialogLayout)
.setPositiveButton(getString(R.string.goto_next), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
page2Walkthrough.show();
dialog.cancel();
}
})
.setNegativeButton(getString(R.string.exit_dialog), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
manager.beginTransaction().show(LaunchActivity.editProfileFragment).commit();
dialog.cancel();
}
});
page1Walkthrough = page1WalkthroughDialogBuilder.create();
使用以下布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rel_layout"
android:layout_height="match_parent"
android:background="@drawable/park_with_people"
mlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
android:text="@string/welcome_hdr"
android:textSize="35sp"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp" />
<uomini.com.wegrokstrasbourg.TextViewWithImages
android:id="@+id/body"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/heading"
android:text="@string/welcome_text"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp" />
</RelativeLayout>
除了按钮有白色背景外,一切都显示得很好:
如何更改背景颜色?
您应该创建一个样式,然后将其应用到 Builder constructor
:
<style name="MyTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/myColor</item>
</style>
然后:
AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyTheme)
...
...
.create();