如何仅更改 TextView 的背景透明度,而不影响整个视图?

huangapple go评论98阅读模式
英文:

How to change only the background alpha of TextView not the whole view?

问题

N.B: 这可能是一个重复的帖子。但之前的解决方案对我没起作用...

我正在尝试仅从RecyclerView适配器内部设置TextView的背景不透明度(不包括textColor和背景)。之前问题的答案对我无效。

之前的建议输出

>情况1:

  1. holder.textView.setBackgroundColor(Color.argb(50, 0,255,0)); //这会将整个视图的透明度设置为alpha(背景 + 文本颜色)。

>情况2:

  1. holder.textView.setAlpha(50); //这也会将整个视图的透明度设置为alpha(背景 + 文本颜色)。

>情况3:

  1. holder.textView.getBackground().setAlpha(alpha); //空指针异常

输出

  1. java.lang.NullPointerException: 尝试调用空对象引用上的 'void android.graphics.drawable.Drawable.setAlpha(int)' 方法

这是我的RecyclerView代码:

  1. ...
  2. @RequiresApi(api = Build.VERSION_CODES.O)
  3. @NonNull
  4. @Override
  5. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  6. TextView textView = new TextView(parent.getContext());
  7. return new ViewHolder(textView);
  8. }
  9. @Override
  10. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  11. holder.textView.setAlpha(50);
  12. }
  13. static class ViewHolder extends RecyclerView.ViewHolder {
  14. private TextView textView;
  15. @RequiresApi(api = Build.VERSION_CODES.O)
  16. public ViewHolder(@NonNull View itemView) {
  17. super(itemView);
  18. this.textView = (TextView) itemView;
  19. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  20. textView.setLayoutParams(layoutParams);
  21. textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
  22. textView.setPadding(4,4,4,4);
  23. textView.setAutoSizeTextTypeUniformWithConfiguration(6,24,1, TypedValue.COMPLEX_UNIT_DIP);
  24. }
  25. }
  26. ...
英文:

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:

  1. holder.textView.setBackgroundColor(Color.argb(50, 0,255,0)); //this set alpha to whole view(both background + textColor).

>Case 2:

  1. holder.textView.setAlpha(50); //this also set alpha to whole view(both background + textColor).

>Case 3:

  1. holder.textView.getBackground().setAlpha(alpha); //NullPointerException

Output

  1. java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setAlpha(int)' on a null object reference

Here is my RecyclerView :

  1. ...
  2. @RequiresApi(api = Build.VERSION_CODES.O)
  3. @NonNull
  4. @Override
  5. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  6. TextView textView = new TextView(parent.getContext());
  7. return new ViewHolder(textView);
  8. }
  9. @Override
  10. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  11. holder.textView.setAlpha(50);
  12. }
  13. static class ViewHolder extends RecyclerView.ViewHolder {
  14. private TextView textView;
  15. @RequiresApi(api = Build.VERSION_CODES.O)
  16. public ViewHolder(@NonNull View itemView) {
  17. super(itemView);
  18. this.textView = (TextView) itemView;
  19. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  20. textView.setLayoutParams(layoutParams);
  21. textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
  22. textView.setPadding(4,4,4,4);
  23. textView.setAutoSizeTextTypeUniformWithConfiguration(6,24,1, TypedValue.COMPLEX_UNIT_DIP);
  24. }
  25. }
  26. ...

答案1

得分: 0

  1. @Override
  2. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  3. ShapeDrawable drawable = new ShapeDrawable();
  4. drawable.getPaint().setColor(Color.GREEN); // 你的颜色;
  5. drawable.getPaint().setAlpha(75); // 背景透明度,范围在0到100之间;
  6. holder.textView.setBackground(drawable);
  7. }
英文:

Try this:

  1. ....
  2. @Override
  3. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  4. ShapeDrawable drawable = new ShapeDrawable();
  5. drawable.getPaint().setColor(Color.GREEN); //Your color;
  6. drawable.getPaint().setAlpha(75); //BackGround Alpha between 0 to 100;
  7. holder.textView.setBackground(drawable);
  8. }

huangapple
  • 本文由 发表于 2020年9月18日 17:23:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63952965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定