【问题标题】:How to change DialogFragment header background color and bottom line color如何更改 DialogFragment 标题背景颜色和底线颜色
【发布时间】:2014-08-27 23:42:37
【问题描述】:

我正在尝试自定义 DialogFragment 的一些样式。

我改变了标题的颜色:

TextView title = (TextView)getDialog().findViewById( android.R.id.title );
title.setTextColor( getResources().getColor( R.color.base_app_color ) );

现在我需要更改标题的背景颜色和底线颜色。

有谁知道那些 android.R.id.???或者我该如何存档?

【问题讨论】:

  • 也许你可以为此使用一个主题?
  • 你可能是对的,但我不知道这些项目的名称是什么,是吗?我也可以使用不同的布局,但我很想改变这些颜色。谢谢@Gumbo
  • 我(也许)找到了分隔符的 ID:android.R.id.titleDivider 让我知道它是否有效。
  • 谢谢,但是那个id不存在android.R.id.titleDivider

标签: android android-alertdialog android-theme


【解决方案1】:

在对话框创建后,你实际上可以找到分隔线:

AlertDialog dialog = builder.show();

// Set title divider color
int titleDividerId = getResources().getIdentifier("titleDivider", "id", "android");
View titleDivider = dialog.findViewById(titleDividerId);
if (titleDivider != null)
    titleDivider.setBackgroundColor(getResources().getColor(android.R.color.holo_purple));

自定义标题的背景稍微复杂一些...您需要在主题中定义alertDialogStyle 定义如何绘制对话框的每个区域。例如:

<style name="Theme.Yours" parent="@android:style/Theme.Holo">
    ...
    <item name="android:alertDialogStyle">@style/AlertDialog_Yours</item>
</style>

<style name="AlertDialog_Yours">
    <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>

这些区域可以是颜色或可绘制对象,为了了解它们是什么,我将您重定向到 a blog post I wrote on the subject(第 5 部分),解释如何为所有内容设置主题。

【讨论】:

  • 谢谢。这就是我要找的@Dadou
【解决方案2】:

如果默认对话框不能满足您的需求,您最好提供自己的自定义布局。然后,您可以在 XML 中使其看起来完全符合您的要求。
这在文档中有很好的解释(参见Creating a Custom Layout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-07
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多