如何自定义AlertDialog

如何自定义AlertDialog

本文介绍了Android:如何自定义AlertDialog,Dialog和Spinner标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义所有对话框,alertDialogs和微调框.我想更改文本的颜色和标题栏的背景.

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

我试图更好地解释:我希望文本(JOLLY BAR DI ...)为白色,标题的背景(从对话框顶部到标题下方的软蓝色线条)为蓝色(#044592).

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

我该怎么办?

推荐答案

好,我几乎解决了这个问题(我在回答我自己的问题,因为它可能会帮助其他人解决与我相同的问题).问题仍然存在于微调器,DatePickers等.

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.

对于AlertDialogs,我这样做:

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:

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>

对于我想看起来像对话框的活动,我这样做是这样的:我在Manifest.xml中声明它们,如下所示:

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

然后,在res/values/styles.xml文件中,我这样做:

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>

我仍然在标题和正文之间得到了淡蓝色的分隔线...仍在处理它.如前所述,我仍然不知道如何处理Spinners,DatePicker等...

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

结果如下:AlertDialog:

That's the result:AlertDialog:

还有一个带有对话框外观的活动:

And an Activity with the look of a dialog:

这篇关于Android:如何自定义AlertDialog,Dialog和Spinner标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:38