我正在使用一个web视图,其中我正在添加一个图像视图。如何将此图像视图的背景设置为透明?

我试过了:

mImageview.setBackgroundResource(R.color.trans);

其中trans→<color name="trans">#00000000 </color>。


当前回答

在XML文件中,设置属性“Alpha”

android:alpha="0.0" // for transparent
android:alpha="1.0" // for opaque

您可以给出十进制的0.0到1.0之间的任何值来应用所需的透明度。例如,0.5透明度对于禁用组件是理想的

其他回答

我遇到的另一个工作选项是设置android:background="@null"

还有一个更简单的方法:

mComponentName.setBackgroundResource(android.R.color.transparent);

或者,作为替代,用下面的代码解析资源ID:

  mComponentName.setBackgroundColor(getResources().getColor(android.R.color.transparent));
ImageView.setBackground(R.drawable.my_background);

ImageView.setBackgroundResource(R.color.colorPrimary);

ImageView.getImageAlpha();

ImageView.setAlpha(125); // transparency

如果你想在代码中使用它,只需:

mComponentName.setBackgroundColor(Color.parseColor("#80000000"));