2012-09-05 25 views
6

Tôi muốn thay đổi màu của tiêu đề màu lam và dòng màu xanh có trong AlertDialog (Chủ đề Holo), tôi có thể thay đổi màu của Tiêu đề bằng cách xem xét android styles.xml và themes.xml nhưng tôi không thể tìm thấy bất kỳ thuộc tính hoặc kiểu nào mà tôi có thể sử dụng/thay đổi để thay đổi màu của XML của tôi.cách thay đổi đường màu xanh trong AlertDialog (Chủ đề Holo)

Sau đây là phong cách mà tôi đang sử dụng để thay đổi màu sắc tiêu đề

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

Trả lời

4

EDIT: tôi đã nhầm lẫn trong câu trả lời ban đầu của tôi.

Thật không may kiểu được sử dụng cho hộp thoại là nội bộ và không thể sử dụng theo cách tương tự như một số yếu tố holo dễ thao tác khác. I have created an open source project for dealing with this in very simple situations (Tôi hy vọng sẽ mở rộng nó xuống đường và làm cho nó nhiều hơn .. legit). Dưới đây là một liên kết đến một câu trả lời mới mà địa chỉ này nhiều hơn trong chiều sâu: How can I change the color of AlertDialog title and the color of the line under it


Đối với hậu thế, đây là câu trả lời cũ ngây thơ của tôi:

Nhìn vào answer posted here. Đối với mục tiêu cụ thể của bạn, bạn sẽ muốn ghi đè kiểu dialogTitleDecorLayout (Tôi nghĩ!) thay vì kiểu listSeparatorTextViewStyle.

Nếu bạn tò mò nơi thiết lập ban đầu, bạn có thể poke quanh tệp themes.xml trong thư mục sdk android của bạn trên máy tính của bạn và tìm kiếm thuộc tính dialogTitleDecorLayout. Điều này sẽ dẫn bạn đến tệp bố cục dialog_title_holo cũng nên có trên máy tính của bạn. Ở đó, bạn sẽ tìm thấy mã sau trong bố cục:

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

Mã lưu ý là tham chiếu @android:drawable/divider_strong_holo. Tệp này cũng tồn tại trên máy tính của bạn và nó phải là một 9patch màu xanh lam. Bạn sẽ cần phải tạo ra một 9patch tương tự của một màu sắc khác nhau. Chúc may mắn! Thậm chí nếu điều này không hoàn toàn thuộc tính right Tôi nghĩ bạn xem những gì bạn có thể cần phải làm ở đây ...

+1

Giải pháp này có phù hợp với bạn không? – Gopinath

+0

Tôi không biết câu trả lời của bạn có giải quyết được http://stackoverflow.com/questions/14439538/how-can-i-change-the-color-of-alertdialog-title-and-the-color-of-the này không -line-under –

+0

@forsubhi Tôi nghĩ nó giải quyết vấn đề đó, nhưng tôi muốn tự mình thử nghiệm nó trước khi đăng câu trả lời chỉ để chắc chắn 100%. (trừ khi bạn đã thử? Trong trường hợp này tôi sẽ vui lòng đăng câu trả lời của tôi) –

1

Có một thư viện mà làm chính xác điều đó cho tất cả các hộp thoại của bạn:

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

+1

Xin chào, cảm ơn con trỏ. Trên thực tế, chúng tôi đã thử sử dụng thư viện này trong dự án của mình, nhưng đã bỏ lại nó.Ít nhất tại thời điểm nó đã giảm ngắn trong việc thay thế các API bản địa, và không thể chứa tất cả các loại hộp thoại chúng tôi đã xây dựng. – Chris

4

sau khi một số mày mò, tôi đã tìm ra một cách để tái thương hiệu hộp thoại thời điểm nó được hiển thị:

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

Lưu ý, rằng giải pháp này có thể phá vỡ bất cứ lúc nào bố trí hệ thống cơ bản cho những thay đổi hộp thoại. Tuy nhiên, nó là hộp thoại hệ thống ban đầu với tất cả các ưu điểm của giải pháp như vậy.

+0

Đó là một hack, nhưng một hack chức năng. Bạn không chắc chắn loại tác động hiệu suất nào sẽ thêm vào ứng dụng? Sẽ thử nghiệm nó và xem nó hoạt động như thế nào trên một chặng đường dài. Cảm ơn cho những nỗ lực! – Kyle

Các vấn đề liên quan