设置AlertBox标题栏背景颜色

时间:2011-05-09 05:47:50

标签: android background alertdialog

如何更改警告框标题栏的背景颜色?

AlertDialog.Builder alert=new AlertDialog.Builder(getParent());
alert.setTitle("sample");
alert.show();

3 个答案:

答案 0 :(得分:10)

最简单的方法是通过创建一个扩展对话框的类来实现对话的子类化,并实现以样式作为参数的构造函数。然后为它制作自己的自定义布局。

显示对话框的代码:

private void showDialog()
{
    Custom_Dialog dialog = new Custom_Dialog(this, R.style.myCoolDialog);

    dialog.setContentView(R.layout.custom_dialog);
    dialog.setTitle("Custom Dialog");

    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");
    ImageView image = (ImageView) dialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.icon);

    dialog.show();  
}

子类的代码:

package com.stackoverflow;

import android.app.Dialog;
import android.content.Context;

public class Custom_Dialog extends Dialog {

    protected Custom_Dialog(Context context, int theme) {
        super(context, theme);
        // TODO Auto-generated constructor stub
    }

}

样式:myCoolDialog.xml

<resources>
    <style name="myCoolDialog" parent="android:Theme.Dialog"> 
        <item name="android:windowBackground">@drawable/blue</item>
        <item name="android:colorForeground">#f0f0</item> 
     </style> 
</resources>

并持续布局:custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

答案 1 :(得分:0)

您可以像这样设置自定义标题

LayoutInflater inflater = this.getLayoutInflater();
View titleView = inflater.inflate(R.layout.custom_title, null);

new AlertDialog.Builder(SubCategoryActivity.this)
                    .setCustomTitle(titleView);

在custom_title布局中,您可以像这样创建自定义标题

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:id="@+id/llsubhead"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/exemptionSubHeading4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:text="Exemption Sub Head"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@color/white" />
    </LinearLayout>
</LinearLayout>

答案 2 :(得分:0)

从@CornflakesDK和@ice精神回答,我认为您可以使用当前的AlertDialog.Builder实现来执行自定义对话框并使其易于维护。

CustomDialogBu​​ilder.java

public class CustomDialogBuilder extends AlertDialog.Builder {

  private View view;

  public CustomDialogBuilder(Context context) {
    super(context);
    view = LayoutInflater.from(getContext()).inflate(R.layout.custom_dialog_title, null);
    setCustomTitle(view);
  }

  @Override
  public Builder setTitle(int titleId) {
    TextView titleTextView = view.findViewById(R.id.exemptionSubHeading4);
    titleTextView.setText(getContext().getString(titleId));
    return this;
  }

  @Override
  public Builder setTitle(CharSequence title) {
    TextView titleTextView = view.findViewById(R.id.exemptionSubHeading4);
    titleTextView.setText(title);
    return this;
  }
}

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <LinearLayout
    android:id="@+id/llsubhead"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:background="@color/black"
    android:orientation="vertical">
    <TextView
      android:id="@+id/exemptionSubHeading4"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:layout_margin="15dp"
      android:layout_gravity="center"
      android:text="Exemption Sub Head"
      android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
      android:textColor="@color/white" />
  </LinearLayout>
</LinearLayout>

在您的活动代码中

 new CustomDialogBuilder(MyActivity.this)
                  .setTitle(R.string.actions)
                  .setItems(R.array.items_actions, (dialog, which) -> {
                    // handle items
                  }).create().show();

然后,您可以在DialogBu​​ilder中使用样式,还可以使用AlertDialog.Builder的功能。