更改Android中的TextView不透明度

问题描述 投票:10回答:5

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

这是我的代码(classicTextTextViewgameSelectorseekbar):

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch){
    classicText.setAlpha(gameSelector.getProgress());
}
android textview opacity seekbar
5个回答
40
投票

您可以这样设置Alpha

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

作为您从搜索栏中获得的字母,它将被设置为文本颜色


11
投票

这对我有用:

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中创建textview:

...
   <!--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。现在,您可以在活动中使用此文本视图,例如:

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

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

5
投票

更改方法至以下

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

0
投票

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

textView.setAlpha();

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


-1
投票

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);
© www.soinside.com 2019 - 2024. All rights reserved.