7

AlertDialog(Holo Theme)に含まれる青いタイトルと青い線の色を変更したいのですが、Androidのstyles.xmlとthemes.xmlを調べてタイトルの色を変更できましたが、プロパティやスタイルが見つかりませんXMLの色を変更するために使用/変更できます。

以下は、タイトルの色を変更するために使用しているスタイルです

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

<style name="myAlertDialogTitleStyle">
    <item name="android:maxLines">1</item>
    <item name="android:scrollHorizontally">true</item>
    <item name="android:textAppearance">@style/myAlertDialogTitleStyleTextAppearance</item>
</style>

<style name="myAlertDialogTitleStyleTextAppearance">
    <item name="android:textSize">22sp</item>
    <item name="android:textColor">#ffff8800</item>
</style>
4

3 に答える 3

4

編集:元の答えを間違えました。

残念ながら、ダイアログに使用されるスタイルは内部的なものであり、他の簡単に操作できるホロ要素と同じ方法で使用することはできません。私は、非常に単純な状況でこれに対処するためのオープン ソース プロジェクトを作成しました(将来的に拡張して、より合法的なものにしたいと考えています)。これをより詳細に解決する新しい回答へのリンクを次に示します: How can I change the color of AlertDialog title and the color of the line under it


後世のために、これが私の素朴な古い答えです:

ここに投稿された回答を見てください。あなたの特定の目標のために、スタイルではなくdialogTitleDecorLayoutスタイルをオーバーライドしたいと思うでしょう(私は思います!listSeparatorTextViewStyle

これが最初にどこに設定されているか知りたい場合は、コンピューターの android sdk フォルダーにある themes.xml ファイルを調べて、dialogTitleDecorLayout属性を検索してください。これdialog_title_holoにより、コンピューターにもあるレイアウトファイルが表示されます。レイアウト内に次のコードがあります。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true">
  <TextView android:id="@android:id/title" style="?android:attr/windowTitleStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="60dip"
    android:paddingLeft="32dip"
    android:paddingRight="32dip"
    android:gravity="center_vertical|left" />
  <ImageView android:id="@+id/titleDivider"
        android:layout_width="match_parent"
        android:layout_height="4dip"
        android:scaleType="fitXY"
        android:gravity="fill_horizontal"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:src="@android:drawable/divider_strong_holo" />
  <FrameLayout
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:foreground="?android:attr/windowContentOverlay">
    <FrameLayout android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  </FrameLayout>
</LinearLayout>

注目のコードは@android:drawable/divider_strong_holo参考です。このファイルはコンピュータにも存在し、青色の 9 パッチになっているはずです。異なる色の同様の 9 パッチを作成する必要があります。幸運を!これが適切な属性ではない場合でも、ここで何をする必要があるかがわかると思います...

于 2012-09-05T07:27:18.330 に答える
4

いくつかいじくり回した後、ダイアログが表示された瞬間にブランドを変更する方法を思いつきました。

private AlertDialog showWithThemeColors() {
    Log.d(TAG, "showWithThemeColors()");
    AlertDialog dialog = this.builder.show();
    try {
        ViewGroup decorView = (ViewGroup) dialog.getWindow().getDecorView();
        FrameLayout windowContentView = (FrameLayout) decorView.getChildAt(0);
        FrameLayout contentView = (FrameLayout) windowContentView.getChildAt(0);
        LinearLayout parentPanel = (LinearLayout) contentView.getChildAt(0);
        LinearLayout topPanel = (LinearLayout) parentPanel.getChildAt(0);
        View titleDivider = topPanel.getChildAt(2);
        LinearLayout titleTemplate = (LinearLayout) topPanel.getChildAt(1);
        TextView alertTitle = (TextView) titleTemplate.getChildAt(1);

        int textColor = this.context.getResources().getColor(R.color.customer_text_on_primary);
        alertTitle.setTextColor(textColor);

        int primaryColor = this.context.getResources().getColor(R.color.customer_primary);
        titleDivider.setBackgroundColor(primaryColor);
    } catch (Exception e) {
        Log.e(TAG, "showWithThemeColors() - Could not re-brand dialog with theme colors.", e);
    }

    return dialog;
}

このソリューションは、ダイアログの基礎となるシステム レイアウトが変更されるたびに壊れる可能性があることに注意してください。ただし、これは、そのようなソリューションのすべての利点を備えた元のシステム ダイアログです。

于 2013-08-26T11:14:05.763 に答える
1

すべてのダイアログに対して正確にそれを行うライブラリがあります。

https://github.com/inmite/android-styled-dialogs

于 2013-06-12T19:45:26.347 に答える