2013-07-21 48 views
13

tôi có các màu sắc sau đây quy định tại color.xml tôi:Android: thay đổi màu sắc của văn bản bị vô hiệu hóa bằng cách sử dụng Theme/Style?

<color name="gold">#d49e43</color> 
<color name="gold_disabled">#80d49e43</color> 

Và chủ đề sau:

<style name="Theme.Example" parent="@style/Theme.Sherlock">  
    <item name="android:textColor">@color/gold</item> 
</style> 

Trong SettingsActivity của tôi, tôi có một CheckBoxPreference và Sở thích điều đó phụ thuộc vào nó. Khi CheckBoxPreference được bỏ chọn, tùy chọn này bị vô hiệu hóa, tuy nhiên, vì màu văn bản màu vàng tùy chỉnh mà tôi đã đặt, nó không bị "chuyển sang màu xám" giống như màu mặc định. Làm cách nào để thay đổi điều này trong XML? Tôi đã thử cài đặt:

<item name="android:textColorPrimaryDisableOnly">@color/gold_disabled</item> 
    <item name="android:textColorPrimaryInverseDisableOnly">@color/gold_disabled</item> 
    <item name="android:textColorPrimaryNoDisable">@color/gold_disabled</item> 
    <item name="android:textColorSecondaryNoDisable">@color/gold_disabled</item> 
    <item name="android:textColorPrimaryInverseNoDisable">@color/gold_disabled</item> 
    <item name="android:textColorSecondaryInverseNoDisable">@color/gold_disabled</item> 

nhưng không có gì có vẻ hiệu quả.

Trả lời

3

tôi figured này ra nhiều hơn hoặc ít hơn một cách tình cờ, nhưng nếu bạn phân lớp Preference và ghi đè lên onBindView(), bạn có thể đạt được "chuyển sang màu xám" có hiệu lực khi một sở thích bị vô hiệu hóa:

@Override 
protected void onBindView(View view) { 
    // TODO Auto-generated method stub 
    super.onBindView(view); 
    TextView title = (TextView)view.findViewById(android.R.id.title); 
    TextView summary = (TextView)view.findViewById(android.R.id.summary); 

    if (title.isEnabled()) { 
     title.setTextColor(getContext().getResources().getColor(R.color.gold)); 
    } 
    else { 
     title.setTextColor(getContext().getResources().getColor(R.color.gold_disabled)); 
    } 

    if (summary.isEnabled()) { 
     summary.setTextColor(getContext().getResources().getColor(R.color.orange)); 
    } 
    else { 
     summary.setTextColor(getContext().getResources().getColor(R.color.orange_disabled)); 
    } 
} 
24

tôi biết Tôi trễ. Tuy nhiên, tôi đã có chính xác cùng một vấn đề và tôi đã cố định ngay bây giờ.

Tôi đã tìm cách sửa lỗi bằng cách chỉ sử dụng các tệp tài nguyên. Tôi tìm thấy câu trả lời ở đây: https://stackoverflow.com/a/17123161/4860513

Về cơ bản, bạn có thể tạo một chọn màu dưới: res/màu/

Lưu ý: Bạn phải tạo màu thư mục nếu nó không tồn tại.

Đối với tôi, tôi đã làm điều đó:

res \ màu \ primary_text_color_selector.xml (Đối với Tiêu đề)

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_enabled="false" android:color="#80000000"/> 
    <item android:color="#FF000000"/> 
</selector> 

res \ màu \ secondary_text_color_selector.xml (Đối với Tóm lược)

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_enabled="false" android:color="#80000000"/> 
    <item android:color="#C0000000"/> 
</selector> 

Sau đó, theo kiểu tùy chọn của tôi, tôi đã làm:

res \ giá trị \ styles.xml

<!-- Preference Screen Theme --> 
<style name="AppTheme_PreferenceScreenStyle"> 
    <item name="android:textColorPrimary">@color/primary_text_color_selector</item> 
    <item name="android:textColorSecondary">@color/secondary_text_color_selector</item> 
</style> 

trong SettingsFragmentActivity.java

public class SettingsFragmentActivity extends PreferenceFragment { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     addPreferencesFromResource(R.xml.settings); 
     PreferenceScreen preferenceScreen = getPreferenceScreen(); 

     mContext = preferenceScreen.getContext(); 
     mContext.setTheme(R.style.AppTheme_PreferenceScreenStyle); 

     ... 
    } 
} 

Bằng cách này, Tiêu đề và tóm tắt được chuyển sang màu xám khi tùy chọn vô hiệu hóa.

Tôi chỉ chia sẻ giải pháp này kể từ khi tôi có cùng một vấn đề và nó có thể giúp một người nào đó

+1

Giải pháp tuyệt vời với tất cả các bước cần thiết để thực hiện nó - đã giúp tôi rất nhiều –

+0

Sẽ làm việc này cho việc thay đổi màu sắc của hộp kiểm hoặc biểu tượng nút radio? Nếu vậy, tên mục trong tệp styles.xml sẽ là gì? –

+0

Đảm bảo "android: textColor" không được xác định hoặc điều này không hoạt động. –