2013-08-08 52 views
5

Tôi đang cố gắng đặt tất cả Kiểu của mình vào Chủ đề cho ứng dụng của mình. Tuy nhiên, tôi đang gặp sự cố khi thay đổi textColor trên TextView. Đây là từ tệp attrs.xml của tôi.Android TextView thay đổi textColor với Theme

<?xml version="1.0" encoding="utf-8"?> 

<declare-styleable name="flat"> 
    <attr name="detail_name_view" format="reference" /> 

</declare-styleable> 

Đây là từ styles.xml tôi tập tin

<style name="flat_theme" parent="android:Theme.NoTitleBar"> 
    <item name="detail_name_view">@style/flat_detail_name</item> 
</style> 
<style name="flat_detail_name" parent="@android:style/Widget.TextView"> 
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item> 
    <item name="android:textColor">@color/detail_group_black</item> 
    <item name="android:textSize">16sp</item> 
    <item name="android:singleLine">true</item> 
    <item name="android:ellipsize">end</item> 
</style> 

Và đây là từ Layout.xml tôi tập tin

<TextView 
     android:id="@+id/detail_name" 
     style="?detail_name_view" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_gravity="left|center_vertical" 
     android:layout_marginLeft="5dp" 
     android:layout_weight="1" 
     android:gravity="left|center_vertical" /> 

Tôi cũng có tệp kê khai được thiết lập chính xác như sau.

<application 
    android:name="com.example.blah" 
    android:icon="@drawable/blah" 
    android:label="@string/app_name" 
    android:theme="@style/flat_theme" 

Màu không bao giờ bị thay đổi thành "@ color/detail_group_black". Tôi thiếu một cái gì đó đơn giản? Hoặc là vấn đề mà Android đã tạo ra một thuộc tính textColor cho TextView và đó là lý do tại sao phong cách không ghi đè nó? Chiến lược chủ đề thuộc tính của tôi dường như hoạt động tốt cho phần còn lại của ứng dụng nhưng không phải là điều này. Bất kỳ ý tưởng? Cảm ơn trước. Ngoài ra, nếu nó có thể được giúp tôi không muốn tạo một lớp TextView tùy chỉnh chỉ cho sự thay đổi này: nó là một dự án khá lớn và tôi không chắc chắn những loại vấn đề tôi có thể chạy vào nếu tôi mở rộng TextView cho mọi điều.

Chỉnh sửa Ngoài ra, nếu tôi thay đổi TextView thành style = "@ style/flat_detail_name_view", nó hoạt động tốt. Lý do tôi không muốn làm điều đó là để tôi có thể thay đổi diện mạo của các bản xem trước này chỉ bằng cách thay đổi chủ đề. Sử dụng style = "? Something_else" hoạt động trên tất cả các chế độ xem khác của tôi, vì vậy tôi thực sự không hiểu điều này và tôi có thể thiếu một số thứ đơn giản?

+0

"? Detail_name_view" trong kiểu TextView là gì? –

+0

style = "? Detail_name_view" là tham chiếu đến thuộc tính detail_name_view. –

Trả lời

2

Tôi đã tìm thấy sự cố của mình, trong trường hợp có ai đó gặp phải tình huống tương tự! Về cơ bản lý do này không được làm việc là vì tôi đã tăng lượt xem với mã sau đây.

private View buildGroupView(ExpandListGroup group) { 
    LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
    View view; 
    switch(group.getType()){ 
     case ExpandListGroup.HEADER : 
      view = inf.inflate(R.layout.three_col_header, null); 
      break; 
     default: 
      view = inf.inflate(R.layout.detail_expandablelist_group_item, null); 
      break; 

    } 

    return view; 
} 

Và trong giao diện getGroupView của Adapter, tôi đã trả lại.

public View getGroupView(int groupPosition, boolean isLastChild, View convertView, 
     ViewGroup parent) { 
    View view = convertView; 
    ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition); 

    view = buildGroupView(group); 

    setGroupViewData(group, view); 

    return view; 
} 

Rõ ràng vấn đề của tôi là tôi cần phải thay thế LayoutInflater

inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 

với ..

LayoutInflater inf = mActivity.getLayoutInflater(); 

Theo đề nghị của CommonsWare ở dưới cùng của bài viết này: Theme/Style is not applied when inflater used with ApplicationContext.

Cảm ơn sự giúp đỡ của bạn.