Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Alpha设置为android按钮_Android - Fatal编程技术网

如何将Alpha设置为android按钮

如何将Alpha设置为android按钮,android,Android,我有三个按钮,共享相同的背景图像,我想使用Alpha禁用其中一个按钮 但当我使用以下代码时: button1.getBackground().setAlpha(45); 它正在更改所有三个按钮的背景。但我只需要一个。 我们可以使用by Alpha()完成吗??或者其他一些我们可以使用的东西,使按钮看起来处于禁用模式。您可以使用AlphaAnimation将alpha设置为任何视图 Button btn; public void onCreate(Bundle savedInstanceSt

我有三个按钮,共享相同的背景图像,我想使用Alpha禁用其中一个按钮

但当我使用以下代码时:

 button1.getBackground().setAlpha(45);
它正在更改所有三个按钮的背景。但我只需要一个。
我们可以使用by Alpha()完成吗??或者其他一些我们可以使用的东西,使按钮看起来处于禁用模式。

您可以使用
AlphaAnimation
将alpha设置为任何视图

Button btn;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main);  
    btn = (Button) findViewById(R.id.main_btn);  
    Drawable d = getResources().getDrawable(R.drawable.imagen);  
    d.setAlpha(60);  
    btn.setBackgroundDrawable(d);  
}
示例代码

Button btn = (Button) findViewById(R.id.button);  
float alpha = 0.45f;
AlphaAnimation alphaUp = new AlphaAnimation(alpha, alpha);
alphaUp.setFillAfter(true);
btn.startAnimation(alphaUp);


在我的例子中,我将一个按钮设置为alpha 75。但所有其他具有相同背景颜色的按钮也都更改为alpha 75。调用
mutate()
解决了这个问题

Pavel Dudka对我的案子很有效

buttonArrivals.getBackground().mutate().setAlpha(180);
buttonDepartures.getBackground().mutate().setAlpha(255);

检查一下,你确定每个按钮都有不同的ID吗?嗨,Lumis,谢谢你的回复,我已经检查过了,每个按钮都有它唯一的ID。非常有趣的是,你可以用这种方式改变所有按钮的背景,谢谢你发布这个问题。
d.setAlpha(60)要求API等级11+注释,下一级设定α(0.5f);setAlpha(int)从API级别1开始就存在,但从级别16开始就被弃用了。setAlpha(float)继续工作。考虑这一点:当用户在设备上禁用动画时,这不起作用。
nextBtn.setAlpha(0.5f); 
buttonArrivals.getBackground().mutate().setAlpha(180);
buttonDepartures.getBackground().mutate().setAlpha(255);