使用attrs.xml文件中的属性以编程方式设置textColor

时间:2017-10-13 03:34:06

标签: java android colors android-recyclerview attr

我有一个RecyclerView,根据收到的优先级设置文本颜色。意思是如果优先级等于1(高),则文本颜色为RED,否则文本颜色将遵循DEFAULT颜色(是android:textColorPrimary或​​android:textColorSecondary )。 但是我无法从attr文件中获取值。

以下是我尝试在代码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代码中进行设置。

  

机器人:文字颜色=&#34;机器人:?textColorPrimary&#34;

<?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代码并不是很擅长,这样做可能看起来很愚蠢,但我很欣赏解决这个问题的建议。

1 个答案:

答案 0 :(得分:0)

为什么不从资源(colors.xml)获取颜色?

onBindViewHolder中,您可以根据优先级值将文字颜色设置为context.getColor(R.color.red)

相关问题