更改 Android 中的 TextView 不透明度

发布于 2024-12-26 05:10:23 字数 630 浏览 2 评论 0原文

因此,我尝试动态更改 Android 应用程序中 TextView 的不透明度。我有一个 seekbar,当我向右滑动拇指时,我在其下分层的 TextView 应该开始变得透明。当拇指到达 seekbar 的一半左右时,文本应该完全透明。我尝试在 TextView 上使用从 View 继承的 setAlpha(float) 方法,但 Eclipse 告诉我 setAlpha() 未定义对于 TextView 类型。我是否以错误的方式调用该方法?或者有其他方法可以改变不透明度吗?

这是我的代码(classicTextTextViewgameSelectorseekbar):

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch){
    classicText.setAlpha(gameSelector.getProgress());
}

So I'm trying to dynamically change the opacity of a TextView in my android app. I have a seekbar and as I slide the thumb to the right, the TextView I have layered under it should start becoming transparent. When the thumb reaches about half way across the seekbar, the text should be completely transparent. I'm trying to use the setAlpha(float) method inherited from View on my TextView, but Eclipse is telling me setAlpha() is undefined for the type TextView. Am I calling the method in the wrong way? Or is there another way to change the opacity?

Here's my code (classicText is the TextView, gameSelector is the seekbar):

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch){
    classicText.setAlpha(gameSelector.getProgress());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

笑忘罢 2025-01-02 05:10:23

您可以像这样将 alpha 设置

int alpha = 0;
((TextView)findViewById(R.id.t1)).setTextColor(Color.argb(alpha, 255, 0, 0));

为从搜索栏获得的 alpha,并将其设置为文本颜色

you can set the alpha like this

int alpha = 0;
((TextView)findViewById(R.id.t1)).setTextColor(Color.argb(alpha, 255, 0, 0));

as the alpha you getting from the seekbar that will be set into the text color

策马西风 2025-01-02 05:10:23

这对我有用:

1. 创建类 AlphaTextView.class

public class AlphaTextView extends TextView {

  public AlphaTextView(Context context) {
    super(context);
  }

  public AlphaTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  public boolean onSetAlpha(int alpha) 
  {
    setTextColor(getTextColors().withAlpha(alpha));
    setHintTextColor(getHintTextColors().withAlpha(alpha));
    setLinkTextColor(getLinkTextColors().withAlpha(alpha));
    getBackground().setAlpha(alpha);
    return true;
  }    
}

2. 添加此内容而不是使用 TextView 在 xml 中创建文本视图:

...
   <!--use complete path to AlphaTextView in following tag-->
   <com.xxx.xxx.xxx.AlphaTextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="sample alpha textview"
         android:gravity="center"
         android:id="@+id/at"
         android:textColor="#FFFFFF"
         android:background="#88FF88"
        />
...

3. 现在您可以在 Activity 中使用此文本视图,例如:

at=(AlphaTextView)findViewById(R.id.at);

at.onSetAlpha(255); // To make textview 100% opaque
at.onSetAlpha(0); //To make textview completely transperent

This worked for me:

1. Create class AlphaTextView.class:

public class AlphaTextView extends TextView {

  public AlphaTextView(Context context) {
    super(context);
  }

  public AlphaTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  @Override
  public boolean onSetAlpha(int alpha) 
  {
    setTextColor(getTextColors().withAlpha(alpha));
    setHintTextColor(getHintTextColors().withAlpha(alpha));
    setLinkTextColor(getLinkTextColors().withAlpha(alpha));
    getBackground().setAlpha(alpha);
    return true;
  }    
}

2. Add this instead of using TextView to create a textview in your xml:

...
   <!--use complete path to AlphaTextView in following tag-->
   <com.xxx.xxx.xxx.AlphaTextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="sample alpha textview"
         android:gravity="center"
         android:id="@+id/at"
         android:textColor="#FFFFFF"
         android:background="#88FF88"
        />
...

3. Now you can use this textview in your activity like:

at=(AlphaTextView)findViewById(R.id.at);

at.onSetAlpha(255); // To make textview 100% opaque
at.onSetAlpha(0); //To make textview completely transperent
夜深人未静 2025-01-02 05:10:23

现在 XML 上有一个属性

android:alpha="0.5"

可以通过布局更改不透明度

Now there is a Attribute on XML as

android:alpha="0.5"

to change the Opacity through the Layout

黯淡〆 2025-01-02 05:10:23

将方法更改为以下

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch)
{
    classicText.setAlpha((float)(gameSelector.getProgress())/(float)(seekBar.getMax()));
}

change method to following

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch)
{
    classicText.setAlpha((float)(gameSelector.getProgress())/(float)(seekBar.getMax()));
}
菩提树下叶撕阳。 2025-01-02 05:10:23

可能已经晚了,但如果有人现在正在寻找这个,您所要做的就是:

textView.setAlpha();

在括号中输入 0 到 1 之间的数字

It may be late but if anyone is looking for this now, all you have to do is:

textView.setAlpha();

in brackets enter number between 0 and 1

三生一梦 2025-01-02 05:10:23

我认为这是一种以编程方式为 TextView 设置 alpha 或不透明度的简单方法。
通过在从 textView 获取 TextColors 之后使用函数 withAlpha

// alpha value between 0..255
// 0 for transparent
classicText.setTextColor(classicText.textColors.withAlpha(100))

I think this is a simple way to set alpha or opacity programmatically for TextView.
By using function withAlpha after getTextColors from textView.

// alpha value between 0..255
// 0 for transparent
classicText.setTextColor(classicText.textColors.withAlpha(100))
夏了南城 2025-01-02 05:10:23

View.setAlpha(float xxx);

xxx - 0 - 255 范围,0 为透明,255 为不透明。

int progress = gameSelector.getProgress();
int maxProgress = gameSelector.getMax();
float opacity = (progress / maxProgress)*255;
classicText.setAlpha(opacity);

View.setAlpha (float xxx);

range of xxx - 0 - 255, 0 being transparent and 255 being opaque.

int progress = gameSelector.getProgress();
int maxProgress = gameSelector.getMax();
float opacity = (progress / maxProgress)*255;
classicText.setAlpha(opacity);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文