2017-10-13 39 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中的默认文本颜色 –

相关问题