setTextColor 可编程使用颜色值
setTextColor programmable using color value
我需要在适配器 class 中设置文本颜色而不是 activity 并使用记录在 colors.xml 文件中的颜色值!
这是代码:对于任何遗漏的东西,我们深表歉意
这是代码:抱歉遗漏任何东西
这是代码:抱歉遗漏任何东西
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
private final LayoutInflater inflater;
ArrayList<HashMap<String, String>> productsHashMapList;
/*int[] images = {
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background,
R.drawable.ic_launcher_foreground
};*/
public MyAdapter(Context context, ArrayList<HashMap<String, String>> productsJsonList){
inflater = LayoutInflater.from(context);
this.productsHashMapList = productsJsonList;
}
@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_row,parent,false);
myViewHolder holder = new myViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(myViewHolder holder, int position) {
holder._productName.setText(productsHashMapList.get(position).get("name"));
holder._productName.setTextColor(getResources().getColor(R.color.colorRed));
}
@Override
public int getItemCount() {
return productsHashMapList.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
ImageView _imgview;
TextView _productName;
public myViewHolder(View itemView) {
super(itemView);
_imgview = (ImageView) itemView.findViewById(R.id.logo);
_productName = (TextView) itemView.findViewById(R.id.productName);
}
}
}
试试
textViewOBJ.setTextColor(ContextCompat.getColor(context, R.color.your_color_code));
仅供参考
你应该通过 Context
.
如何?
通过您的适配器 class。 #构造函数
ArrayList<HashMap<String, String>> productsHashMapList;
Context context;
public MyAdapter(Context contextOBJ, ArrayList<HashMap<String, String>> productsJsonList){
this.context=contextOBJ;
inflater = LayoutInflater.from(context);
this.productsHashMapList = productsJsonList;
}
textView.setTextColor(getResources().getColor(R.color.color));
根据 prashant verma 的回答,在构造函数中初始化上下文。
然后在代码中使用它。
textView.setTextColor(context.getResources().getColor(R.color.color));
首先在 Activity 中通过适配器的构造函数传递上下文,同时在 activity 中初始化适配器并在适配器中设置上下文:
this.context = context; // set this in the constructor.
然后,设置文本颜色:
textView.setTextColor(context.getResources().getColor(R.color.white)); //whatever your color
始终在 RecyclerView.ViewHolder
class 中设置文本颜色,而不是在适配器 class 的 onBindViewHolder
方法中设置文本颜色,看看下面的解决方案
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView YOUR_TEXT_VIEW;
MyViewHolder(View view) {
super(view);
YOUR_TEXT_VIEW = view.findViewById(R.id.YOUR_TEXT_VIEW);
YOUR_TEXT_VIEW.setTextColor(ContextCompat.getColor(YOUR_APP_CONTEXT, R.color.your_color_code));
}
}
我需要在适配器 class 中设置文本颜色而不是 activity 并使用记录在 colors.xml 文件中的颜色值!
这是代码:对于任何遗漏的东西,我们深表歉意 这是代码:抱歉遗漏任何东西 这是代码:抱歉遗漏任何东西
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
private final LayoutInflater inflater;
ArrayList<HashMap<String, String>> productsHashMapList;
/*int[] images = {
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_background,
R.drawable.ic_launcher_foreground
};*/
public MyAdapter(Context context, ArrayList<HashMap<String, String>> productsJsonList){
inflater = LayoutInflater.from(context);
this.productsHashMapList = productsJsonList;
}
@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.list_row,parent,false);
myViewHolder holder = new myViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(myViewHolder holder, int position) {
holder._productName.setText(productsHashMapList.get(position).get("name"));
holder._productName.setTextColor(getResources().getColor(R.color.colorRed));
}
@Override
public int getItemCount() {
return productsHashMapList.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
ImageView _imgview;
TextView _productName;
public myViewHolder(View itemView) {
super(itemView);
_imgview = (ImageView) itemView.findViewById(R.id.logo);
_productName = (TextView) itemView.findViewById(R.id.productName);
}
}
}
试试
textViewOBJ.setTextColor(ContextCompat.getColor(context, R.color.your_color_code));
仅供参考
你应该通过 Context
.
如何?
通过您的适配器 class。 #构造函数
ArrayList<HashMap<String, String>> productsHashMapList;
Context context;
public MyAdapter(Context contextOBJ, ArrayList<HashMap<String, String>> productsJsonList){
this.context=contextOBJ;
inflater = LayoutInflater.from(context);
this.productsHashMapList = productsJsonList;
}
textView.setTextColor(getResources().getColor(R.color.color));
根据 prashant verma 的回答,在构造函数中初始化上下文。 然后在代码中使用它。
textView.setTextColor(context.getResources().getColor(R.color.color));
首先在 Activity 中通过适配器的构造函数传递上下文,同时在 activity 中初始化适配器并在适配器中设置上下文:
this.context = context; // set this in the constructor.
然后,设置文本颜色:
textView.setTextColor(context.getResources().getColor(R.color.white)); //whatever your color
始终在 RecyclerView.ViewHolder
class 中设置文本颜色,而不是在适配器 class 的 onBindViewHolder
方法中设置文本颜色,看看下面的解决方案
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView YOUR_TEXT_VIEW;
MyViewHolder(View view) {
super(view);
YOUR_TEXT_VIEW = view.findViewById(R.id.YOUR_TEXT_VIEW);
YOUR_TEXT_VIEW.setTextColor(ContextCompat.getColor(YOUR_APP_CONTEXT, R.color.your_color_code));
}
}