如何仅更改 TextView 的背景 alpha 而不是整个视图?
How to change only the background alpha of TextView not the whole view?
N.B:这可能是重复的 post。但是以前的解决方案都不适合我...
我正在尝试从 RecyclerView 适配器(不是 textColor 和背景)内部设置 TextView(仅)背景不透明度。上一个问题的答案对我不起作用。
先前建议的输出
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
输出
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setAlpha(int)' on a null object reference
这是我的 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);
}
}
...
试试这个:
....
@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);
}
N.B:这可能是重复的 post。但是以前的解决方案都不适合我...
我正在尝试从 RecyclerView 适配器(不是 textColor 和背景)内部设置 TextView(仅)背景不透明度。上一个问题的答案对我不起作用。
先前建议的输出
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
输出
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setAlpha(int)' on a null object reference
这是我的 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);
}
}
...
试试这个:
....
@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);
}