首页 文章

如何更改AlertDialog Headers 的颜色以及其下方的线条颜色

提问于
浏览
102

我使用此命令更改了AlertDialog Headers 的颜色

alert.setTitle( Html.fromHtml("<font color='#FF7F27'>Set IP Address</font>"));

但我想改变 Headers 下出现的线条的颜色;我怎样才能做到这一点 ?

注意:我不想使用自定义布局

screenshot of the desired effect

14 回答

  • 0

    如果要为警报对话框创建自定义布局

    然后你可以轻松地添加这种方式来改变颜色

    <LinearLayout
        android:id="@+id/DialogTitleBorder"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:layout_below="@id/mExitDialogDesc"
        android:background="#4BBAE3"            <!--change color easily -->
        >
    
    </LinearLayout>
    
  • 131

    不幸的是,这不是一项特别简单的任务 . In my answer here,我详细介绍了如何通过检查Android使用的父样式,创建新图像以及根据原始图像创建新样式来调整 ListSeparator 的颜色 . 不幸的是,与 ListSeparator 的样式不同, AlertDialog 主题是内部的,因此不能作为父样式引用 . 没有简单的方法来改变那条小蓝线!因此,您需要使用自定义对话框 .

    如果这不是你的一杯茶... don't give up! 我很不安,没有简单的方法可以做到这一点所以我在github上设置了一个小项目来制作快速定制的全息式对话框(假设手机支持Holo风格) . You can find the project here: https://github.com/danoz73/QustomDialog

    它应该很容易从无聊的蓝色到激动人心的橙色!

    enter image description here

    该项目基本上是使用自定义对话框构建器的示例,在示例中,我创建了一个自定义视图,似乎可以满足您在原始问题中提供的IP地址示例 .

    使用 QustomDialog ,为了创建一个基本对话框( Headers ,消息), Headers 或分隔符具有所需的不同颜色,您可以使用以下代码:

    private String HALLOWEEN_ORANGE = "#FF7F27";
    
    QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(v.getContext()).
        setTitle("Set IP Address").
        setTitleColor(HALLOWEEN_ORANGE).
        setDividerColor(HALLOWEEN_ORANGE).
        setMessage("You are now entering the 10th dimension.");
    
    qustomDialogBuilder.show();
    

    并且为了添加自定义布局(例如,添加小IP地址 EditText ),添加

    setCustomView(R.layout.example_ip_address_layout, v.getContext())
    

    使用您设计的布局构建器(可以在github中找到IP示例) . 我希望这有帮助 . Many thanks to Joseph Earl and his answer here .

  • 0

    Divider color:

    这是一个hack有点,但它对我来说很好,它没有任何外部库(至少在Android 4.4上) .

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.dialog)
           .setIcon(R.drawable.ic)
           .setMessage(R.string.dialog_msg);
    //The tricky part
    Dialog d = builder.show();
    int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
    View divider = d.findViewById(dividerId);
    divider.setBackgroundColor(getResources().getColor(R.color.my_color));
    

    您可以在alert_dialog.xml文件中找到更多对话框ID . 例如 . android:id/alertTitle 用于更改 Headers 颜色...

    UPDATE: Title color

    用于更改 Headers 颜色的Hack:

    int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
    TextView tv = (TextView) d.findViewById(textViewId);
    tv.setTextColor(getResources().getColor(R.color.my_color));
    
  • 10

    检查这对你有用...

    public void setCustomTitle (View customTitleView)
    

    你从以下链接获得详细信息 .

    http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCustomTitle%28android.view.View%29

    CustomDialog.java

    Dialog alert = new Dialog(this);
        alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alert.setContentView(R.layout.title);
        TextView msg = (TextView)alert.findViewById(R.id.textView1);
        msg.setText("Hello Friends.\nIP address : 111.111.1.111");
        alert.show();
    

    title.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set IP address"
        android:textColor="#ff0000"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
    <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_marginTop="5dp"
        android:background="#00ff00"
        />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#775500"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    

    enter image description here

  • 0

    这将设置 Headers ,图标和分隔符的颜色 . 必须随着任何新的Android版本而改变 .

    public static void colorAlertDialogTitle(AlertDialog dialog, int color) {
        int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
        if (dividerId != 0) {
            View divider = dialog.findViewById(dividerId);
            divider.setBackgroundColor(color);
        }
    
        int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
        if (textViewId != 0) {
            TextView tv = (TextView) dialog.findViewById(textViewId);
            tv.setTextColor(color);
        }
    
        int iconId = dialog.getContext().getResources().getIdentifier("android:id/icon", null, null);
        if (iconId != 0) {
            ImageView icon = (ImageView) dialog.findViewById(iconId);
            icon.setColorFilter(color);
        }
    }
    

    记得在调用此方法之前调用dialog.show() .

  • 74

    通过遵循Dialog source code,我发现通过膨胀dialog_title_holo.xml布局在类MidWindow中生成 Headers . 所以 mTitleView 的Id是 title ,分隔符的Id是 titleDivider .

    我们可以通过 android.R.id.title 访问 title 的Id .

    并通过 Resources.getSystem().getIdentifier("titleDivider","id", "android"); 访问 titleDivider 的ID

    我用来改变 Headers 方向和改变颜色的最终代码是:

    TextView mTitle = (TextView)findViewById(android.R.id.title);
    mTitle.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
    int x = Resources.getSystem().getIdentifier("titleDivider","id", "android");
    View titleDivider = findViewById(x);
    titleDivider.setBackgroundColor(getContext().getResources().getColor(R.color.some_color));
    
  • 0

    如果你不想要一个“库”,你可以使用这个严重的黑客:

    ((ViewGroup)((ViewGroup)getDialog().getWindow().getDecorView()).getChildAt(0)) //ie LinearLayout containing all the dialog (title, titleDivider, content)
    .getChildAt(1) // ie the view titleDivider
    .setBackgroundColor(getResources().getColor(R.color.yourBeautifulColor));
    

    这是经过测试并在4.x上工作;未经测试,但如果我的记忆力良好,它应该适用于2.x和3.x.

  • 21

    如果您使用 custom title layout 则可以像alertDialog.setCustomTitle(customTitle)一样使用它;

    例如

    on UI thread used dialog like 
    
     LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
     View customTitle=inflater.inflate(R.layout.customtitlebar, null);
     AlertDialog.Builder d=new AlertDialog.Builder(this);
     d.setCustomTitle(customTitle);
     d.setMessage("Message");
     d.setNeutralButton("OK", null);
     d.show();
    
    
    customtitlebar.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#525f67">
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/ic_launcher"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true" >
        </ImageView>
    
        <TextView
            android:id="@+id/customtitlebar"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:textColor="#ffffff"
            android:text="Title Name"
            android:padding="3px"
            android:textStyle="bold" 
            android:layout_toRightOf="@id/icon"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical"/>
    
         <ImageView
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#ff0000" 
            android:layout_below="@id/icon"><!-- This is line below the title -->
        </ImageView>
    
    </RelativeLayout>
    
  • 4

    继续这个答案:https://stackoverflow.com/a/15285514/1865860,我从@ daniel-smith分出了漂亮的github repo并做了一些改进:

    • 改进示例活动

    • 改进了布局

    • 修复 setItems 方法

    • 将分频器添加到 items_list
      单击

    • 关闭对话框

    • 支持 setItems 方法中的禁用项目

    • listItem 触摸反馈

    • 可滚动对话框消息

    链接:https://github.com/dentex/QustomDialog

  • 7

    我提出了另一个解决方案,在一个地方处理对话框的样式,你不必担心何时应用它 - 对话框显示/未显示,这可能会导致错误(应该调用requestFocus或类似的东西; P) .

    用法示例:

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    AlertDialog dialog = builder.create(); //or builder.show()
    DialogViewDecorator.decorate(dialog, android.R.color.holo_red_light); //can also set the defaut color in the class
    

    执行:

    public class DialogViewDecorator {
    
    private static final
    @ColorRes int DEFAULT_TITLE_DIVIDER_COLOR = android.R.color.holo_orange_light;
    
    public static void decorate(Dialog dialog) {
        decorate(dialog, DEFAULT_TITLE_DIVIDER_COLOR);
    }
    
    /**
     * Sets the title divider color when the view is shown by setting DialogInterface.OnShowListener on the dialog.
     * <p/>
     * If you want to do other things onShow be sure to extend OnDecoratedDialogShownListener(call super.show(...)!)
     * and call {@link #decorate(Dialog, int, OnDecoratedDialogShownListener)}.
     *
     * @param dialog
     * @param titleDividerColor
     */
    public static void decorate(Dialog dialog, final int titleDividerColor) {
        decorate(dialog, titleDividerColor, new OnDecoratedDialogShownListener(titleDividerColor));
    }
    
    /**
     * Method for setting a extended implementation of OnDecoratedDialogShownListener. Don't forget to call super
     * or the titleDividerColor wont be applied!
     *
     * @param dialog
     * @param titleDividerColor
     * @param OnShowListener
     * @param <T>
     */
    public static <T extends OnDecoratedDialogShownListener> void decorate(Dialog dialog, int titleDividerColor, T OnShowListener) {
        if (dialog == null || titleDividerColor <= 0) { return; }
    
        if (dialog.isShowing()) {
            setTitleDividerColor(dialog, titleDividerColor);
        } else {
            dialog.setOnShowListener(OnShowListener);
        }
    }
    
    private static void setTitleDividerColor(DialogInterface dialogInterface, int titleDividerColor) {
        try {
            Dialog dialog = (Dialog) dialogInterface;
            int dividerId = dialog.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
            View divider = dialog.findViewById(dividerId);
            if (divider != null) {
                divider.setBackgroundColor(dialog.getContext().getResources().getColor(titleDividerColor));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public static class OnDecoratedDialogShownListener implements DialogInterface.OnShowListener {
        private int titleDividerColor;
    
        public OnDecoratedDialogShownListener() {
            this.titleDividerColor = DEFAULT_TITLE_DIVIDER_COLOR;
        }
    
        public OnDecoratedDialogShownListener(int titleDividerColor) {
            this.titleDividerColor = titleDividerColor;
        }
    
        @Override
        public void onShow(DialogInterface dialogInterface) {
            setTitleDividerColor(dialogInterface, titleDividerColor);
        }
    }}
    
  • 0

    在onCreateView类中,我把它放在:

    Dialog d = getDialog();
        d.setTitle(Html.fromHtml("<font color='#EC407A'>About</font>"));
        int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
        View divider = d.findViewById(dividerId);
        divider.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    

    colorPrimary链接到我们的colors.xml文件,用于存储所有颜色 . 此外 d.setTitle 提供了一种设置 Headers 颜色的hacky方法 .

  • 0

    不使用对话框中的分隔符,而是使用自定义布局中的视图,并将布局设置为对话框中的自定义布局 .

    custom_popup.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
        <com.divago.view.TextViewMedium
            android:id="@+id/txtTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="AlertDialog"
            android:textColor="@android:color/black"
            android:textSize="20sp" />
    
        <View
            android:id="@+id/border"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@id/txtTitle"
            android:background="@color/txt_dark_grey" />
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/border"
            android:scrollbars="vertical">
    
            <com.divago.view.TextViewRegular
                android:id="@+id/txtPopup"
                android:layout_margin="15dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </ScrollView>
    </RelativeLayout>
    

    activity.java:

    public void showPopUp(String title, String text) {
    
        LayoutInflater inflater = getLayoutInflater();
        View alertLayout = inflater.inflate(R.layout.custom_popup, null);
    
        TextView txtContent = alertLayout.findViewById(R.id.txtPopup);
        txtContent.setText(text);
    
        TextView txtTitle = alertLayout.findViewById(R.id.txtTitle);
        txtTitle.setText(title);
    
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setView(alertLayout);
        alert.setCancelable(true);
    
        alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    
        AlertDialog dialog = alert.create();
        dialog.show();
    }
    
  • 2

    如果您正在使用扩展对话框,请使用:

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    
  • 1
    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.BLACK);
    
        String title = context.getString(R.string.agreement_popup_message);
        SpannableStringBuilder ssBuilder = new SpannableStringBuilder(title);
        ssBuilder.setSpan(
                foregroundColorSpan,
                0,
                title.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );
    
    AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(context);
    alertDialogBuilderUserInput.setTitle(ssBuilder)
    

相关问题