改变窗框的颜色

时间:2010-01-07 02:13:26

标签: android

如何更改此对话框的颜色?我尝试了很多东西,没有任何作用。

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">               
    <item name="android:windowNoTitle">true</item>
  </style>
</resources>

2 个答案:

答案 0 :(得分:11)

你的意思是白框?我认为它是9-patch drawable的一部分你可以在SDK_FOLDER \ platforms \ android-sdkversion \ data \ res \ values中查找Theme.Dialog的构建方式 然后是styles.xml和themes.xml

正如我所说,白框是背景图像的一部分。它的panel_background.9.png所以如果你想改变框架 - 你需要不同的背景图像+需要在样式设置中覆盖它。

 <item name="android:windowBackground">@android:drawable/panel_background</item> 

你需要定义一个从Theme.Dialog派生的样式并拥有它

<item name="android:windowBackground">@drawable/your_drawable</item> 

所以如果你把styles.xml放在像

这样的东西
<style name="NewBorderDialogTheme" parent="android:Theme.Dialog">
 <item name="android:windowBackground">@drawable/your_drawable</item> 
</style>

放置新的drawable并将您的活动设置为新主题 - 您应该看到新的边框

答案 1 :(得分:0)

如果您想以编程方式执行此操作,请尝试以下代码:

你需要做什么:

  

屏幕上显示AlertDialog时,会调用OnShowListener。因此,通过添加dialog.setOnShowListener(this),您就可以自定义AlertDialog

<强>代码:

// Create AlertDialog
AlertDialog.Builder adb = new AlertDialog.Builder(context1);
    adb.setTitle(context1.getString(R.string.app_name))
    .setMessage(message)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
});
AlertDialog dialog = adb.create();

// Make some UI changes for AlertDialog
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(final DialogInterface dialog) {

        // Add or create your own background drawable for AlertDialog window
        Window view = ((AlertDialog)dialog).getWindow();
        view.setBackgroundDrawableResource(R.drawable.your_drawable);

        // Customize POSITIVE, NEGATIVE and NEUTRAL buttons.
        Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE);
        positiveButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
        positiveButton.setTypeface(Typeface.DEFAULT_BOLD);
        positiveButton.invalidate();

        Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
        negativeButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
        negativeButton.setTypeface(Typeface.DEFAULT_BOLD);
        negativeButton.invalidate();

        Button neutralButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEUTRAL);
        neutralButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
        neutralButton.setTypeface(Typeface.DEFAULT_BOLD);
        neutralButton.invalidate();
    }
});
相关问题