英文:
How to change only the background alpha of TextView not the whole view?
问题
N.B: 这可能是一个重复的帖子。但之前的解决方案对我没起作用...
我正在尝试仅从RecyclerView适配器内部设置TextView的背景不透明度(不包括textColor和背景)。之前问题的答案对我无效。
之前的建议输出
>情况1:
holder.textView.setBackgroundColor(Color.argb(50, 0,255,0)); //这会将整个视图的透明度设置为alpha(背景 + 文本颜色)。
>情况2:
holder.textView.setAlpha(50); //这也会将整个视图的透明度设置为alpha(背景 + 文本颜色)。
>情况3:
holder.textView.getBackground().setAlpha(alpha); //空指针异常
输出
java.lang.NullPointerException: 尝试调用空对象引用上的 'void android.graphics.drawable.Drawable.setAlpha(int)' 方法
这是我的RecyclerView代码:
...
@RequiresApi(api = Build.VERSION_CODES.O)
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
TextView textView = new TextView(parent.getContext());
return new ViewHolder(textView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.textView.setAlpha(50);
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
@RequiresApi(api = Build.VERSION_CODES.O)
public ViewHolder(@NonNull View itemView) {
super(itemView);
this.textView = (TextView) itemView;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(layoutParams);
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setPadding(4,4,4,4);
textView.setAutoSizeTextTypeUniformWithConfiguration(6,24,1, TypedValue.COMPLEX_UNIT_DIP);
}
}
...
英文:
N.B: This might be a duplicate post. But no previous solution worked for me...
I'm trying to set TextView (only)background opacity from inside a RecyclerView Adapter(not both the textColor & background). Answer of previous question didn't worked for me.
Output of Previous Suggestions
>Case 1:
holder.textView.setBackgroundColor(Color.argb(50, 0,255,0)); //this set alpha to whole view(both background + textColor).
>Case 2:
holder.textView.setAlpha(50); //this also set alpha to whole view(both background + textColor).
>Case 3:
holder.textView.getBackground().setAlpha(alpha); //NullPointerException
Output
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setAlpha(int)' on a null object reference
Here is my RecyclerView :
...
@RequiresApi(api = Build.VERSION_CODES.O)
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
TextView textView = new TextView(parent.getContext());
return new ViewHolder(textView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.textView.setAlpha(50);
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
@RequiresApi(api = Build.VERSION_CODES.O)
public ViewHolder(@NonNull View itemView) {
super(itemView);
this.textView = (TextView) itemView;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(layoutParams);
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setPadding(4,4,4,4);
textView.setAutoSizeTextTypeUniformWithConfiguration(6,24,1, TypedValue.COMPLEX_UNIT_DIP);
}
}
...
答案1
得分: 0
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ShapeDrawable drawable = new ShapeDrawable();
drawable.getPaint().setColor(Color.GREEN); // 你的颜色;
drawable.getPaint().setAlpha(75); // 背景透明度,范围在0到100之间;
holder.textView.setBackground(drawable);
}
英文:
Try this:
....
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ShapeDrawable drawable = new ShapeDrawable();
drawable.getPaint().setColor(Color.GREEN); //Your color;
drawable.getPaint().setAlpha(75); //BackGround Alpha between 0 to 100;
holder.textView.setBackground(drawable);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论