2011-05-09 83 views
9

如何更改警报框标题栏的背景颜色?设置警报框标题栏背景颜色

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

可能是这样的帮助:[在Android中创建自定义提醒框。](http://stackoverflow.com/questions/3928562/creating-custom-alert-box-in-android) – 2011-05-09 05:52:17

+0

最佳答案我到目前为止:http://blog.supenta.com/2014/07/02/how-to-style-alertdialogs-like-a-pro/ – 2017-02-26 18:33:30

回答

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> 

和持续的布局:c ustom_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> 
+0

嗨CornflakesDK,我想在alertDialog框中显示内容列表。标题背景颜色已更改,但不包含dislpay内容列表。我使用此代码来显示内容列表alert.setItems(列表,新的DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog,int pos){ TODO自动生成的方法存根}}); – Jeeva 2011-05-09 06:46:39

+0

然后更改布局。 如果需要,您可以添加自己的按钮和button.OnClickListeners。 – CornflakesDK 2011-05-09 06:49:16

+0

谢谢,listView工作正常。 – Jeeva 2011-05-09 07:12:58

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> 
0

从答案@CornflakesDK和自定义标题@冰灵,我以为你可以使用当前的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。