java - 如何在运行时了解布局背景和更改文本颜色 android studio
问题描述
大家好,我正在开发一个在运行时设置每个项目背景颜色的应用程序,我的默认文本颜色是黑色,但如果背景也是黑色,我看不到我的文本,请问我怎么知道的背景颜色运行时回收器视图中的一个项目,如果太暗,相应地更改文本颜色使文本颜色反之亦然
这是我编写的实用程序类 `public class Utils {
// ColorDrawable colorDrawable = new ColorDrawable()
private static ColorDrawable colorDrawable[] = {
new ColorDrawable(Color.parseColor("#2e86c1")),
new ColorDrawable(Color.parseColor("#6c3483")),
new ColorDrawable(Color.parseColor("#239b56")),
new ColorDrawable(Color.parseColor("#641e16")),
new ColorDrawable(Color.parseColor("#58d68d")),
new ColorDrawable(Color.parseColor("#7e5109")),
new ColorDrawable(Color.parseColor("#17202a")),
new ColorDrawable(Color.parseColor("#7d3c98")),
new ColorDrawable(Color.parseColor("#d2b4de")),
new ColorDrawable(Color.parseColor("#f39c12")),
new ColorDrawable(Color.parseColor("#154360")),
new ColorDrawable(Color.parseColor("#641e16")),
new ColorDrawable(Color.parseColor("#145a32")),
};
public static ColorDrawable getRandomColor() {
int r = new Random().nextInt(12);
return colorDrawable[r];
}
} the adapter class
公共类 NoteAdapter 扩展 ListAdapter<Note, NoteViewHolder> {
Context context;
public static final int TYPE_ADS = 0;
public static final int TYPE_NORMAL = 1;
int clickCount = 0;
public NoteAdapter(Context context) {
super(itemCallback);
this.context = context;
}
static DiffUtil.ItemCallback<Note> itemCallback = new DiffUtil.ItemCallback<Note>() {
@Override
public boolean areItemsTheSame(@NonNull Note oldItem, @NonNull Note newItem) {
return oldItem.getClass().equals(newItem.getClass());
}
@Override
public boolean areContentsTheSame(@NonNull Note oldItem, @NonNull Note newItem) {
return oldItem.equals(newItem);
}
};
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
holder.binding.re.setBackground(Utils.getRandomColor());
final Note note = (Note) getItem(position);
if (note != null) {
holder.binding.setNote(note);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clickCount == 3) {
// show ads
showAds.booleanMutableLiveData.setValue(true);
clickCount = 0;
} else {
clickCount += 1;
Intent intent = new Intent(context, NoteDetailActivity.class);
intent.putExtra("subject", note.getSubject());
intent.putExtra("body", note.getBody());
intent.putExtra("date", note.getDate());
intent.putExtra("priority", note.getPriority());
intent.putExtra("id", note.getId());
context.startActivity(intent);
}
}
});
}
public Note note(int position) {
return (Note) getItem(position);
}
class ViewHolder extends RecyclerView.ViewHolder {
NoteItem2Binding binding;
public ViewHolder(@NonNull NoteItem2Binding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
} `
解决方案
推荐阅读
- networkx - pygraphviz/networkx external label (xlabel)
- in-app-purchase - Does Play Billing Library support UPI with IAP?
- c# - SignalR send message from client to server error
- python - How OpenCV's ConnectedComponent algorithm works?
- python - 如何使用 python 在邮件正文中发送 html 内容?
- c - 如何使用 C 编程添加和显示从串行端口获取的数据的小时、分钟、秒
- javascript - ReactJS:如何防止 array.push 中的重复值?
- python - 如何使用python删除图像中图形周围的白色边框/边缘?
- python - 如何在 Python 中打开 .txt 文件进行数据存储?
- javascript - 如何重置我的许多选择框值?