Android:如何自定义AlertDialog,Dialog an Spinner标题

时间:2013-01-28 13:17:00

标签: android android-spinner android-alertdialog android-dialog

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

enter image description here

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

我该怎么做?

1 个答案:

答案 0 :(得分:7)

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

对于AlertDialogs,我做了类似这样的事情:

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>

对于我想看起来像Dialogs的活动,我喜欢这样: 我在Manifest.xml中声明它们如下:

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

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

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

这是结果: AlertDialog:

enter image description here

带有对话框的活动:

enter image description here

相关问题