在Android中更改TextView不透明度

Mic*_*rew 10 android opacity textview seekbar

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

这是我的代码(classicTextTextView,gameSelectorseekbar):

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

Pra*_*tik 40

你可以像这样设置alpha

int alpha = 0;
((TextView)findViewById(R.id.t1)).setTextColor(Color.argb(alpha, 255, 0, 0));
Run Code Online (Sandbox Code Playgroud)

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


Hir*_*ral 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;
  }    
}
Run Code Online (Sandbox Code Playgroud)

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"
        />
...
Run Code Online (Sandbox Code Playgroud)

3.现在您可以在您的活动中使用此textview,例如:

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

at.onSetAlpha(255); // To make textview 100% opaque
at.onSetAlpha(0); //To make textview completely transperent
Run Code Online (Sandbox Code Playgroud)


RAI*_*INA 10

现在 XML 上有一个属性:

安卓:阿尔法=“0.5”

通过布局更改不透明度


jee*_*eet 5

改变方法如下

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