2012-09-05 14 views
6

我想要更改AlertDialog(Holo Theme)中出現的藍色標題和藍線的顏色,我可以通過查找來更改標題的顏色android styles.xml和themes.xml,但我找不到任何屬性或樣式,我可以使用/更改來改變我的XML的顏色。如何更改AlertDialog中的藍線(Holo Theme)

以下是我使用更改標題顏色

<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

編輯樣式:我在我原來的答案是錯誤的。

不幸的是用於對話框的樣式是內部的,不能像其他一些易於操作的全息元素一樣使用I have created an open source project for dealing with this in very simple situations(我希望將它擴展到更多的道路上,讓它更合理)。這是一個新的答案的鏈接,解決這個更深入的:How can I change the color of AlertDialog title and the color of the line under it


對於後人,這裏是我天真的老回答:

看那answer posted here。爲了您的特定目標,您希望重寫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參考。該文件也存在於您的計算機上,它應該是藍色的9patch。您將需要創建一個不同顏色的類似9patch。祝你好運!即使這是不完全正確的屬性,我覺得你看你可能需要在這裏做什麼?

+1

此解決方案是否適合您? – Gopinath

+0

我不知道你的答案是否解決這個問題http://stackoverflow.com/questions/14439538/how-can-i-change-the-color-of-alertdialog-title-and-the-color-of-the -line-under –

+0

@forsubhi我認爲它確實解決了這個問題,但是我想在發佈答案之前自己測試一下,以便100%確定。 (除非你已經嘗試過?在這種情況下,我會很樂意發佈我的答案) –

1

有這不正是對所有的對話框庫:

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

+1

嗨,謝謝你的指針。實際上,我們嘗試在我們的項目中使用這個庫,但是再次丟棄它。至少在替代原生API方面不足,並且不能適應我們已經構建的各種對話框。 – Chris

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; 
} 

注意,此解決方案可以打破任何時候在對話框改變底層的系統佈局。但是,這是具有此類解決方案所有優點的原始系統對話框。

+0

這是一個破解,但功能劈。不確定這會增加應用程序對性能的影響嗎?去測試它,看看它如何長期運作。感謝您的努力! – Kyle