Android - Change custom dialog title background

孤街浪徒 提交于 2019-12-20 12:38:45

问题


I am creating a custom dialog and I want to know how to change the background of the title bar.

I've tried two approaches:

1 - I've tried the AlertDialog.Builder method 'setCustomTitle'. I created a simple layout view comprising of a textview with layout width and height 'match_parent' and background color. When I run the app, only the top half of the title bar is showing the background color. The bottom half is still showing the default theme background color. Does anyone know why?

2 - I've created my own dialog theme. Ive created a style with parent inheritance to '@android:style/Theme.Holo.Light.Dialog'. I've then passed that in the AlertDialog.Builder constructor - new AlertDialog.Builder(this, R.style.test_dialog). It seems good but somehow the dialog is wrapped within a dialog. A square box is surrounding the dialog. Does anyone know why?


回答1:


You can create a style like,

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

And you can instantiate dialog:

Dialog dialog=new Dialog(this,R.style.cust_dialog);
dialog.setContentView(R.layout.fragment_features_dialog);
dialog.setTitle(R.string.features);

Now the dialog shows up with black title background color.




回答2:


The dialog-wrapped-within-a-dialog appearance is caused by the dialog's window background. Every dialog has this, but the default Android dialogs have the window background set to transparent. To do this, add this item in your custom dialog theme:

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



回答3:


I am using Mono Android(Xamarin); am showing you another alternative that am using in my app to create a dialog in a fragment:

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);

Resource.Layout.listitem_custom_dialog: this is custom listview layout, here is the xml file:

<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(); 

Android.Resource.Id.Title: this is the id of the textview containing the dialog title. and it is predefined by android. this way you will get a dialog that you can style in way you want.




回答4:


Create an xml layout file for your title and inflate it and set to to the AlertDialog as

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



回答5:


You can just set custom title like this

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

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

and in custom_title layout you can create custom title like this

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


来源:https://stackoverflow.com/questions/9302449/android-change-custom-dialog-title-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!