问题描述
我有一个按钮,如下所示:
I have a button as in the following:
<Button
android:text="Submit"
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
在我的 onCreate()
事件中,我像这样调用 Button01:
In my onCreate()
event, I am calling Button01 like this:
setContentView(R.layout.main);
View Button01 = this.findViewById(R.id.Button01);
Button01.setOnClickListener(this);
应用中有一个背景,我想在这个提交按钮上设置一个不透明度.如何为此视图设置不透明度?是我可以在java端设置,还是可以在main.xml文件中设置?
There is a background in the application, and I want to set an opacity on this submit button. How can I set an opacity for this view? Is it something that I can set on the java side, or can I set in the main.xml file?
在 Java 方面,我尝试了 Button01.mutate().SetAlpha(100)
,但它给了我一个错误.
On the java side I tried Button01.mutate().SetAlpha(100)
, but it gave me an error.
推荐答案
我刚刚发现了您的问题,同时遇到了与 TextView 类似的问题.我能够通过扩展 TextView 并覆盖 onSetAlpha
来解决它.也许你可以尝试用你的按钮做类似的事情:
I just found your question while having the similar problem with a TextView. I was able to solve it, by extending TextView and overriding onSetAlpha
. Maybe you could try something similar with your button:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
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));
return true;
}
}
这篇关于如何在 Android 中为视图设置不透明度(Alpha)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!