2017-10-13 38 views
0

我有一個RecyclerView,根據收到的優先級設置文本顏色。如果優先級等於1(高),則文本顏色爲紅色,否則文本顏色將遵循DEFAULT顏色(,即android:textColorPrimary或​​android:textColorSecondary)。 但我無法從attr文件獲取值。使用attrs.xml文件中的屬性以編程方式設置textColor

下面是我試圖得到它的代碼RecyclerViewAdapter文件

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    context = parent.getContext(); 
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_layout, 
      parent, false); 

    TextView projectTextView = (TextView) itemView.findViewById(R.id.text_project); 
    TextView timeTextView = (TextView) itemView.findViewById(R.id.text_time); 

    return new ViewHolder(itemView, projectTextView, timeTextView, 
      new ViewHolder.OnItemClickListener() { 
     @Override 
     public void onItemClick(int position) { 
      RecyclerViewAdapter.this.itemClickListener.onItemClick(filteredList.get(position)); 
     } 
    }); 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    final Message message = filteredList.get(position); //my Message object 

    holder.projectTextView.setText(message.getProject()); 
    holder.timeTextView.setText(message.getTime()); 

    //Get attribute colour from attr file 
    TypedValue typedValue = new TypedValue(); 
    int[] attrs = new int[]{android.R.attr.textColorPrimary, android.R.attr.textColorSecondary}; 
    TypedArray a = context.obtainStyledAttributes(typedValue.data, attrs); 
    int txtColorPrimary = a.getDimensionPixelSize(0, -1); 
    int txtSecondaryPrimary = a.getDimensionPixelSize(1, -1); 
    a.recycle(); 

    //Set the text colour to red if the priority is high ("1") 
    if(message.getPriority().equals("1")){ 
     holder.projectTextView.setTextColor(Color.RED); 
     holder.timeTextView.setTextColor(Color.RED); 
    }else{ 
     //Else set the text colour to default colour 
     holder.projectTextView.setTextColor(txtColorPrimary); //<-- this is not working 
     holder.timeTextView.setTextColor(txtSecondaryPrimary); //<-- this is not working 
    } 


} 

下面的工作是在attrs.xml我想從

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="Theme"> 
     <!-- The most prominent text color. --> 
     <attr name="textColorPrimary" format="reference|color" /> 
     <!-- Secondary text color. --> 
     <attr name="textColorSecondary" format="reference|color" /> 
     <!-- Tertiary text color. --> 
     <attr name="textColorTertiary" format="reference|color" /> 
    </declare-styleable> 
</resources> 

我只知道獲取的價值的代碼如何在佈局中設置它,但我不知道如何在java代碼中完成它。

機器人:文字顏色= 「機器人:textColorPrimary」

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingStart="16dp" android:paddingLeft="16dp" 
    android:paddingEnd="16dp" android:paddingRight="16dp" 
    android:paddingTop="16dp" 
    android:paddingBottom="16dp"> 


     <TextView 
      android:id="@+id/text_project" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:textColor="?android:textColorPrimary" 
      android:maxLines="1" 
      android:ellipsize="end"/> 

     <TextView 
      android:id="@+id/text_time" 
      android:layout_marginLeft="8dp" 
      android:layout_marginStart="8dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/text_project" 
      android:layout_toEndOf="@id/text_project" 
      android:textStyle="italic" 
      android:textColor="?android:textColorSecondary" 
      android:maxLines="1" 
      android:ellipsize="end" 
      android:gravity="end"/> 


</RelativeLayout> 

,如果我的代碼似乎錯了,我向您道歉。我承認我並不擅長使用android代碼,這樣做似乎很愚蠢,但我很欣賞解決這個問題的建議。

+0

在attrs.xml中定義顏色有什麼特別的要求嗎? –

+0

嗨,是的。我認爲以這種方式管理代碼比較容易,因爲我只是從attrs.xml文件中提取參考顏色 –

+0

我建議您使用樣式。聲明2種樣式:'primaryText'&'secondaryText',其中聲明消息的文本顏色。從代碼中你可以改變textView的風格。請參閱[這裏](https://developer.android.com/guide/topics/ui/look-and-feel/themes.html) –

回答

0

爲什麼不從資源中獲取顏色(colors.xml)?

onBindViewHolder中,您可以根據您的優先級值將文本顏色設置爲context.getColor(R.color.red)

+0

Hi @Aung Ko Ou,我已經更新了我的問題,所以請回頭參考。我遇到的問題實際上是獲取android屬性,** textPrimaryColor **和** textSecondaryColor **並將它們設置在我的RecyclerViewAdapter文件中。我沒有在colors.xml中添加顏色,因爲我覺得它是多餘的,因爲我希望它是已存儲在android attrs.xml中的默認文本顏色 –

相關問題