【问题标题】:Android support alert dialog change header background color?Android支持警报对话框更改标题背景颜色?
【发布时间】:2016-01-06 12:06:35
【问题描述】:

color/colorPrimary 是一些橙色,我希望标题有它。但我成功地更改了标题文本颜色,这很容易。我想更改标题背景的颜色。这是我目前所拥有的:

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">

    <item name="android:textColorPrimary">@color/colorPrimary</item>
    <item name="android:windowTitleBackgroundStyle">@style/dialog_title_style</item>
    <item name="android:alertDialogStyle">@style/AlertDialog_Sphinx</item>
    <item name="colorAccent">@color/colorPrimary</item>




</style>
<style name="dialog_title_style" >
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:padding">100dp</item>
</style>

<style name="AlertDialog_Sphinx">
    <item name="android:fullDark">@color/colorPrimary</item>
    <item name="android:topDark">@color/colorPrimary</item>
    <item name="android:centerDark">@color/colorPrimary</item>
    <item name="android:bottomDark">@color/colorPrimary</item>
    <item name="android:fullBright">@color/colorPrimary</item>
    <item name="android:topBright">@color/colorPrimary</item>
    <item name="android:centerBright">@color/colorPrimary</item>
    <item name="android:bottomBright">@color/colorPrimary</item>
    <item name="android:bottomMedium">@color/colorPrimary</item>
    <item name="android:centerMedium">@color/colorPrimary</item>
</style>

public class MyDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder=new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle).setTitle("Naslov")
            .setMessage("Poruka......................................................")
            .setIcon(android.R.drawable.ic_menu_help)
            .setPositiveButton("OK",null);
    return builder.create();
  }
}

我正在使用 AlertDialog 的支持版本。(23.1.0)这种方式对话框看起来更像是新版本的 android(材料设计)上的对话框

 compile 'com.android.support:appcompat-v7:23.1.0'
 compile 'com.android.support:design:23.1.0'

我希望该标题是颜色/colorPrimary(橙色)背景。

【问题讨论】:

    标签: android android-fragments android-dialogfragment android-styles


    【解决方案1】:

    在最新的 API 级别中,警报对话框没有单独的标题。它有一个单独的视图和一个分隔标题文本和消息的分隔符。您可以更改标题文本和消息文本颜色以及分隔线颜色。此外,您可以更改整个警报对话框的背景,而不仅仅是它的标题部分。

    作为一种解决方法,您可以做的是:不要设置标题文本,而是使用带有文本的图像,然后是消息。这样,分隔线将消失,图像看起来像标题。 基本上是一个自定义警报对话框。

    查看this post 了解如何在警报对话框中添加图像。

    【讨论】:

      【解决方案2】:

      您可以为警报对话框创建自定义视图。

      LayoutInflater inflater = getLayoutInflater();
      View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus());
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setView(dialoglayout);
      builder.show();
      

      【讨论】:

      • 肯定是一个选项,但我认为 headerBackground:#F56321 是更简单的选项。但到目前为止,所有样式选项都没有帮助。
      【解决方案3】:

      您可以查看此材质对话框库here。它非常易于使用,也非常可定制。

      【讨论】:

      • 我会尽量不要仅仅因为设计而使用任何库。但是我的网页有这样的对话框,标题是橙色的,正文和页脚是白色的。它认为完成这样的事情会很好。另外,我认为它很容易改变,就像在网络上一样:)
      • 我暂时不会尝试,但如果使用您的库很容易做到这一点,我会考虑将其作为一种选择。
      • 似乎没有标题背景选项,但您将广告放在这里对您的图书馆有好处。
      • 这不是我的图书馆,但看看你可以制作的自定义主题对话框,它可能会有所帮助。如果您不使用 3rd 方库,也没关系。
      猜你喜欢
      • 2017-05-10
      • 2013-01-07
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多