Android-更改自定义对话框标题背景

Android-更改自定义对话框标题背景,android,dialog,customdialog,Android,Dialog,Customdialog,我正在创建一个自定义对话框,我想知道如何更改标题栏的背景 我尝试了两种方法: 1-我尝试了AlertDialog.Builder方法“setCustomTitle”。我创建了一个简单的布局视图,由一个文本视图组成,该文本视图具有布局宽度和高度“匹配父对象”以及背景色。当我运行应用程序时,只有标题栏的上半部分显示背景色。下半部分仍然显示默认的主题背景色。有人知道为什么吗 2-我创建了自己的对话主题。我创建了一个带有父继承的样式,“@android:style/Theme.Holo.Light.Di

我正在创建一个自定义对话框,我想知道如何更改标题栏的背景

我尝试了两种方法:

1-我尝试了AlertDialog.Builder方法“setCustomTitle”。我创建了一个简单的布局视图,由一个文本视图组成,该文本视图具有布局宽度和高度“匹配父对象”以及背景色。当我运行应用程序时,只有标题栏的上半部分显示背景色。下半部分仍然显示默认的主题背景色。有人知道为什么吗

2-我创建了自己的对话主题。我创建了一个带有父继承的样式,“@android:style/Theme.Holo.Light.Dialog”。然后,我在AlertDialog.Builder构造函数-new AlertDialog.Builder(这是R.style.test_对话框)中传递了它。看起来不错,但不知怎么的,对话框被包装在了一个对话框中。对话框周围有一个方形框。
有人知道为什么吗?

你可以创造一种风格,比如

<style name="cust_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleStyle">@style/dialog_title_style</item>
  </style>

<style name="dialog_title_style" parent="android:Widget.TextView">
    <item name="android:background">@android:color/black</item>
    <item name="android:padding">10dp</item>
</style>

现在对话框显示为黑色标题背景色

对话框内的对话框包装外观是由对话框的窗口背景引起的。每个对话框都有这个功能,但是默认的Android对话框的窗口背景设置为透明。为此,请在自定义对话框主题中添加此项目:

<style name="CustomDialog" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style> 

@android:彩色/透明
我使用的是Mono Android(Xamarin);我向您展示了我在我的应用程序中使用的另一个选项,用于在片段中创建对话框:

Dialog itemDialog = new Dialog(this.Activity);  
TextView alertTitle=(TextView)itemDialog.Window.DecorView.FindViewById(Android.Resource.Id.Title);

alertTitle.SetTextColor(Android.Graphics.Color.Blue);
alertTitle.SetBackgroundColor(Android.Graphics.Color.Orange);
itemDialog.SetContentView(Resource.Layout.listview_custom_dialog);
string[] options = new string[] { "Open", "Mark as Unread","Mute","View    
Profile","Block Connection","Delete Conversation" };
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this.Activity,  
Resource.Layout.listitem_custom_dialog,Resource.Id.textViewDialogDescription,   
options);
Dialog itemDialog=新建对话框(this.Activity);
TextView alertTitle=(TextView)itemDialog.Window.DecorView.FindViewById(Android.Resource.Id.Title);
alertTitle.SetTextColor(Android.Graphics.Color.Blue);
SetBackgroundColor(Android.Graphics.Color.Orange);
itemDialog.SetContentView(Resource.Layout.listview\u自定义\u对话框);
字符串[]选项=新字符串[]{“打开”、“标记为未读”、“静音”、“查看”
配置文件“,”块连接“,”删除会话“};
ArrayAdapter=新的ArrayAdapter(this.Activity,
Resource.Layout.listitem_自定义_对话框,Resource.Id.textViewDialogDescription,
选择权);
Resource.Layout.listitem_自定义_对话框:这是自定义listview布局,下面是xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >
<TextView
    android:id="@+id/textViewDialogDescription"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:background="#ffffff"
    android:textColor="#386B96"
    android:paddingLeft="4dp"
    android:textSize="14dp" />
</RelativeLayout>

ListView lv = itemDialog.FindViewById<ListView> 
(Resource.Id.listViewDialogItems);
lv.Adapter = adapter;
adapter.NotifyDataSetChanged();
itemDialog.SetCancelable(true);
itemDialog.SetTitle("Conversation");
itemDialog.Show(); 

ListView lv=itemDialog.FindViewById
(Resource.Id.listViewDialogItems);
低压适配器=适配器;
adapter.NotifyDataSetChanged();
itemDialog.SetCancelable(真);
itemDialog.SetTitle(“对话”);
itemDialog.Show();
Android.Resource.Id.Title:这是包含对话框标题的文本视图的Id。它是由android预定义的。
通过这种方式,您将获得一个对话框,可以按照您想要的方式进行样式设置。

为您的标题创建一个xml布局文件,并将其放大并设置为AlertDialog as

View view = getLayoutInflater().inflate(R.layout.cust_dialog_title, null);
alertDialog.setCustomTitle(view);

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

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

new AlertDialog.Builder(SubCategoryActivity.this)
                    .setCustomTitle(titleView);
<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>
在自定义标题布局中,您可以创建如下自定义标题

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

new AlertDialog.Builder(SubCategoryActivity.this)
                    .setCustomTitle(titleView);
<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>


您好,谢谢您的回复。为什么Alert.Builder会显示不同的UI?AlertDialog.Builder testBuilder;testBuilder=newalertdialog.Builder(这个R.style.cust\u对话框);setView(布局);我正在使用父主题theme.Holo.Light.Dialog。另外,当我使用Alertdialog.Builder时,对话框被包装在一个对话框中。有人知道为什么吗,可以完美地工作。