我在想有没有人能帮我一下。我试图创建一个自定义AlertDialog。为了做到这一点,我在styles.xml中添加了以下代码行
<resources>
<style name="CustomAlertDialog" parent="android:Theme.Dialog.Alert">
<item name="android:windowBackground">@drawable/color_panel_background</item>
</style>
</resources>
Color_panel_background.9.png位于可绘制文件夹。这也可以在Android SDK res文件夹。
以下是主要活动。
package com.customdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class CustomDialog extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTheme(R.style.CustomAlertDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("HELLO!");
builder .setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//dialog.cancel();
}
});
AlertDialog alertdialog = builder.create();
alertdialog.show();
}
}
为了将主题应用到AlertDialog,我必须将主题设置为当前上下文。
然而,我似乎不能让应用程序显示自定义的AlertDialog。有人能帮我一下吗?
我想这是不可能的。至少不是和建造者。我正在使用1.6和Builder.create()中的实现是:
public AlertDialog create() {
final AlertDialog dialog = new AlertDialog(P.mContext);
P.apply(dialog.mAlert);
[...]
}
它调用AlertDialog的“非主题感知”构造函数,看起来像这样:
protected AlertDialog(Context context) {
this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}
在AlertDialog中有第二个构造函数用于更改主题:
protected AlertDialog(Context context, int theme) {
super(context, theme);
[...]
}
建造者就是不叫。
如果Dialog非常通用,我会试着编写AlertDialog的子类,调用第二个构造函数并使用该类而不是生成器机制。
您可以通过修改活动的主题属性....来覆盖由活动生成的DialogFragments所使用的默认主题
在AndroidManifest.xml中设置活动的主题。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld">
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"> <!-- set all Activity themes to your custom theme -->
.....
</application>
</manifest>
在values/styles.xml中,重写用于确定派生的DialogFragments使用什么主题的项
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
<!-- override the default theme for DialogFragments -->
<item name="android:dialogTheme">@style/AppTheme.Dialog</item>
</style>
.....
</resources>
在values/styles.xml中,定义并配置你想要用于DialogFragments的主题
<?xml version="1.0" encoding="utf-8"?>
<resources>
.....
<!--
configure your custom theme for DialogFragments...
-->
<style name="AppTheme.Dialog" parent="Theme.AppCompat.Dialog.MinWidth">
<!-- override the default theme for DialogFragments spawned by this DialogFragment -->
<item name="android:dialogTheme">@style/AppTheme.Dialog</item>
<!--
OPTIONAL: override the background for the dialog...i am using a dark theme,
and for some reason, there is no themes for dialogs with dark backgrounds,
so, i made my own.
-->
<item name="android:windowBackground">@drawable/dialog__window_background</item>
<!--
add the title to the dialog's theme. you can remove it later by using
DialogFragment.setStyle()
-->
<item name="android:windowNoTitle">false</item>
<item name="windowNoTitle">?android:windowNoTitle</item>
</style>
.....
</resources>
可选:如果你使用一个黑暗的主题,并覆盖android:windowBackground,就像我在AppTheme。对话框,然后添加一个可绘制的/dialog__window_background.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="?dialogCornerRadius" />
<solid android:color="?android:colorBackground" />
</shape>
</inset>
当你初始化Builder时,你可以直接分配一个主题:
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity(), R.style.MyAlertDialogTheme);
然后在values/styles.xml中自定义主题
<!-- Alert Dialog -->
<style name="MyAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorBackground">@color/alertDialogBackground</item>
<item name="android:windowBackground">@color/alertDialogBackground</item>
</style>
我在博客上写过一篇关于如何用XML样式文件配置AlertDialog布局的文章。主要的问题是不同的布局参数需要不同的样式定义。下面是一个基于Holo Light平台19版AlertDialog样式的样板文件,该样式文件应该涵盖一系列标准布局方面,如文本大小和背景颜色。
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
...
<item name="android:alertDialogTheme">@style/MyAlertDialogTheme</item>
<item name="android:alertDialogStyle">@style/MyAlertDialogStyle</item>
...
</style>
<style name="MyBorderlessButton">
<!-- Set background drawable and text size of the buttons here -->
<item name="android:background">...</item>
<item name="android:textSize">...</item>
</style>
<style name="MyButtonBar">
<!-- Define a background for the button bar and a divider between the buttons here -->
<item name="android:divider">....</item>
<item name="android:dividerPadding">...</item>
<item name="android:showDividers">...</item>
<item name="android:background">...</item>
</style>
<style name="MyAlertDialogTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
</style>
<style name="MyAlertTextAppearance">
<!-- Set text size and color of title and message here -->
<item name="android:textSize"> ... </item>
<item name="android:textColor">...</item>
</style>
<style name="MyAlertDialogTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowTitleStyle">@style/MyAlertDialogTitle</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
<item name="android:windowIsFloating">true</item>
<item name="android:textAppearanceMedium">@style/MyAlertTextAppearance</item>
<!-- If you don't want your own button bar style use
@android:style/Holo.Light.ButtonBar.AlertDialog
and
?android:attr/borderlessButtonStyle
instead of @style/MyButtonBar and @style/MyBorderlessButton -->
<item name="android:buttonBarStyle">@style/MyButtonBar</item>
<item name="android:buttonBarButtonStyle">@style/MyBorderlessButton</item>
</style>
<style name="MyAlertDialogStyle">
<!-- Define background colors of title, message, buttons, etc. here -->
<item name="android:fullDark">...</item>
<item name="android:topDark">...</item>
<item name="android:centerDark">...</item>
<item name="android:bottomDark">...</item>
<item name="android:fullBright">...</item>
<item name="android:topBright">...</item>
<item name="android:centerBright">...</item>
<item name="android:bottomBright">...</item>
<item name="android:bottomMedium">...</item>
<item name="android:centerMedium">...</item>
</style>