Android: How to customize AlertDialog, Dialog an S

2020-06-06 06:37发布

I want to customize all dialogs, alertDialogs and spinner. I want to change the color of text and the background of the title bar.

enter image description here

I try to explain better: I want the text (JOLLY BAR DI ...) to be white and the background of the title (from the top of the dialog to the soft-blue line below the title included) to be blue (#044592).

How can I do it?

1条回答
够拽才男人
2楼-- · 2020-06-06 07:08

Ok, I've almost solved the problem (I'm answering my own question because it may help others with the same problem as mine). The problem remains for spinners, DatePickers etc.

For the AlertDialogs I do something like this:

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this, AlertDialog.THEME_HOLO_LIGHT);
//Then I do what I need with the builder (except for setTitle();
LayoutInflater inflater = getLayoutInflater();
View view=inflater.inflate(R.layout.dialog_custom_title, null);
TextView title = (TextView)view.findViewById(R.id.myTitle);
title.setText("Title I want to show");
builder.setCustomTitle(view);
AlertDialog alert = builder.create();
alert.show();

dialog_custom_titile.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#044592" >

   <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myTitle"
        android:layout_width="fill_parent"
        android:textSize="20dp"
        android:layout_height="70dp"
        android:layout_marginLeft="25dp"
        android:paddingTop="22dp"
        android:textColor="@android:color/white"
        android:textStyle="bold" />

</RelativeLayout>

for the Activities that I want to look like Dialogs I do like this: I declare them in the Manifest.xml as follows:

<activity
     android:name="com.aveschini.AnotherActivity"
     android:screenOrientation="portrait"
     android:label="My Dialog"
     android:theme="@style/MyDialog"
     android:windowSoftInputMode="adjustPan" />

then, in the res/values/styles.xml file I do like this:

    <style name="AppTheme" parent="android:Theme.Holo.Light" />

    <style name="MyDialog" parent="android:Theme.Holo.Light.Dialog">
        <item name="android:windowTitleStyle">@style/DialogWindowTitle</item>
    </style>

    <style name="DialogWindowTitle">
        <item name="android:textAppearance">@style/TextAppearance.DialogWindowTitle</item>
        <item name="android:background">#044592</item>
    </style>

    <style name="TextAppearance.DialogWindowTitle" parent="android:TextAppearance">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">20sp</item>
    </style>

I still get the light blue divider between the title and the body... still working on it. As already said, I still don't know how to deal with Spinners, DatePicker, etc...

That's the result: AlertDialog:

enter image description here

And an Activity with the look of a dialog:

enter image description here

查看更多
登录 后发表回答