猿问

如何更改AlertDialog的主题

如何更改AlertDialog的主题

我想知道是否有人能帮我。我正在尝试创建一个自定义AlertDialog。为了做到这一点,我在styes.xml中添加了以下代码行

<resources>
 <style name="CustomAlertDialog" parent="android:Theme.Dialog.Alert">
  <item name="android:windowBackground">@drawable/color_panel_background</item>
 </style></resources>
  • png位于可绘制文件夹中。这也可以在AndroidSDKRES文件夹中找到。

以下是主要活动。

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。有人能帮我解决这个问题吗?


潇湘沐
浏览 1399回答 3
3回答

慕妹3146593

我写了一个文章在我的博客中,介绍如何用XML样式文件配置AlertDialog的布局。主要问题是,对于不同的布局参数,需要不同的样式定义。下面是一个基于HoloLight平台版本19的AlertDialog样式的样板,用于样式文件,它应该涵盖一系列标准布局方面,如文本大小和背景色。<style&nbsp;name="AppBaseTheme"&nbsp;parent="android:Theme.Holo.Light"> &nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:alertDialogTheme">@style/MyAlertDialogTheme</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:alertDialogStyle">@style/MyAlertDialogStyle</item> &nbsp;&nbsp;&nbsp;&nbsp;...</style><style&nbsp;name="MyBorderlessButton"> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;Set&nbsp;background&nbsp;drawable&nbsp;and&nbsp;text&nbsp;size&nbsp;of&nbsp;the&nbsp;buttons&nbsp;here&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:background">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:textSize">...</item></style><style&nbsp;name="MyButtonBar"> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;Define&nbsp;a&nbsp;background&nbsp;for&nbsp;the&nbsp;button&nbsp;bar&nbsp;and&nbsp;a&nbsp;divider&nbsp;between&nbsp;the&nbsp;buttons&nbsp;here&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:divider">....</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:dividerPadding">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:showDividers">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:background">...</item></style><style&nbsp;name="MyAlertDialogTitle"> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:maxLines">1</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:scrollHorizontally">true</item></style><style&nbsp;name="MyAlertTextAppearance"> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;Set&nbsp;text&nbsp;size&nbsp;and&nbsp;color&nbsp;of&nbsp;title&nbsp;and&nbsp;message&nbsp;here&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:textSize">&nbsp;...&nbsp;</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:textColor">...</item></style><style&nbsp;name="MyAlertDialogTheme"> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:windowBackground">@android:color/transparent</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:windowTitleStyle">@style/MyAlertDialogTitle</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:windowContentOverlay">@null</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:windowIsFloating">true</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:textAppearanceMedium">@style/MyAlertTextAppearance</item> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;If&nbsp;you&nbsp;don't&nbsp;want&nbsp;your&nbsp;own&nbsp;button&nbsp;bar&nbsp;style&nbsp;use&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@android:style/Holo.Light.ButtonBar.AlertDialog &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?android:attr/borderlessButtonStyle &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instead&nbsp;of&nbsp;@style/MyButtonBar&nbsp;and&nbsp;@style/MyBorderlessButton&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:buttonBarStyle">@style/MyButtonBar</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:buttonBarButtonStyle">@style/MyBorderlessButton</item></style><style&nbsp;name="MyAlertDialogStyle"> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;Define&nbsp;background&nbsp;colors&nbsp;of&nbsp;title,&nbsp;message,&nbsp;buttons,&nbsp;etc.&nbsp;here&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:fullDark">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:topDark">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:centerDark">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:bottomDark">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:fullBright">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:topBright">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:centerBright">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:bottomBright">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:bottomMedium">...</item> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:centerMedium">...</item></style>
随时随地看视频慕课网APP

相关分类

Android
我要回答