RecyclerView TextView文本颜色已更改

时间:2015-03-02 19:16:16

标签: android

制作一些足球应用,我的ActivitiesFragmentsRecyclerViews很少,但只是在这种情况下我的文字颜色发生了变化: enter image description here

该片段正确显示徽标和名称。

但是在RecyclerView中,你几乎看不到玩家的名字,因为它太白了,其他3个小文字也难以阅读。 看起来文字改变了它的颜色以适应某些黑色主题,而通过我的所有应用程序它默认为Theme.AppCompat.Light.DarkActionBar

编辑所有内容时看起来很好:

enter image description here

并且一个视图持有者的代码看起来也很好:

<?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="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:text="12: Tyler Blackett"
        android:id="@+id/txt_players_name"
        android:layout_marginLeft="12dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="8dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/txt_players_position"
        android:layout_below="@+id/txt_players_name"
        android:layout_alignLeft="@+id/txt_players_name"
        android:layout_alignStart="@+id/txt_players_name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/txt_players_nationality"
        android:layout_below="@+id/txt_players_position"
        android:layout_alignLeft="@+id/txt_players_position"
        android:layout_alignStart="@+id/txt_players_position" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/txt_players_value"
        android:layout_below="@+id/txt_players_nationality"
        android:layout_alignLeft="@+id/txt_players_nationality"
        android:layout_alignStart="@+id/txt_players_nationality" />


</RelativeLayout>

编辑:来自Adapter的一些代码。我多次使用这个适配器(情况0,1,2)并且仅在这种情况下它改变了文本的颜色:

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder viewHolder = null;
        switch(viewType){
            case 0:
                view = inflater.inflate(R.layout.row_teams, parent, false);
                viewHolder = new ViewHolder0(view);
                break; //working fine
            case 1:
                {...} //working fine
            case 2:
                {...} //working fine
            case 3:
                view = inflater.inflate(R.layout.row_players, parent, false);
                viewHolder = new ViewHolder3(view);
        }
        return viewHolder;
    }

在onBind上我只是将.setText调用到TextView,没什么不寻常的。 持有人类:

public class ViewHolder3 extends RecyclerView.ViewHolder {

        TextView mPlayerName;
        TextView mPosition;
        TextView mNationality;
        TextView mValue;

        public ViewHolder3(View itemView) {
            super(itemView);
            mPlayerName = (TextView) itemView.findViewById(R.id.txt_players_name);
            mPosition = (TextView) itemView.findViewById(R.id.txt_players_position);
            mNationality = (TextView) itemView.findViewById(R.id.txt_players_nationality);
            mValue = (TextView) itemView.findViewById(R.id.txt_players_value);
        }

2 个答案:

答案 0 :(得分:15)

我遇到了同样的问题,这是因为我在使用RecyclerView的{​​{1}}创建的LayoutInflaterContext中使用,而不是当前Application

将其更改为从Activity创建后,Activity颜色又变回黑色。

答案 1 :(得分:1)

只需在XML布局中为android:textColor设置TextView属性即可。这样您就可以根据需要更改文本的颜色。