更改 Android 中的 TextView 不透明度
因此,我尝试动态更改 Android 应用程序中 TextView
的不透明度。我有一个 seekbar
,当我向右滑动拇指时,我在其下分层的 TextView
应该开始变得透明。当拇指到达 seekbar
的一半左右时,文本应该完全透明。我尝试在 TextView
上使用从 View 继承的 setAlpha(float)
方法,但 Eclipse 告诉我 setAlpha()
未定义对于 TextView
类型。我是否以错误的方式调用该方法?或者有其他方法可以改变不透明度吗?
这是我的代码(classicText
是 TextView
,gameSelector
是 seekbar
):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以像这样将 alpha 设置
为从搜索栏获得的 alpha,并将其设置为文本颜色
you can set the alpha like this
as the alpha you getting from the seekbar that will be set into the text color
这对我有用:
1. 创建类 AlphaTextView.class:
2. 添加此内容而不是使用 TextView 在 xml 中创建文本视图:
3. 现在您可以在 Activity 中使用此文本视图,例如:
This worked for me:
1. Create class AlphaTextView.class:
2. Add this instead of using TextView to create a textview in your xml:
3. Now you can use this textview in your activity like:
现在 XML 上有一个属性
android:alpha="0.5"
可以通过布局更改不透明度
Now there is a Attribute on XML as
android:alpha="0.5"
to change the Opacity through the Layout
将方法更改为以下
change method to following
可能已经晚了,但如果有人现在正在寻找这个,您所要做的就是:
在括号中输入 0 到 1 之间的数字
It may be late but if anyone is looking for this now, all you have to do is:
in brackets enter number between 0 and 1
我认为这是一种以编程方式为 TextView 设置 alpha 或不透明度的简单方法。
通过在从 textView 获取 TextColors 之后使用函数 withAlpha。
I think this is a simple way to set alpha or opacity programmatically for TextView.
By using function withAlpha after getTextColors from textView.
View.setAlpha(float xxx);
xxx - 0 - 255 范围,0 为透明,255 为不透明。
View.setAlpha (float xxx);
range of xxx - 0 - 255, 0 being transparent and 255 being opaque.