Android: How to customize AlertDialog, Dialog an Spinner titles

前提是你 提交于 2019-12-04 14:08:40

问题


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

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:


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:

And an Activity with the look of a dialog:



来源:https://stackoverflow.com/questions/14562894/android-how-to-customize-alertdialog-dialog-an-spinner-titles

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