Remove background drawable programmatically in And

2019-01-21 08:44发布

I want to remove the background drawable (@drawable/bg) programmatically. Is there a way to do that?

Currently, I have the following XML in my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget29"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/bg">

</RelativeLayout>

11条回答
啃猪蹄的小仙女
2楼-- · 2019-01-21 08:57

I try this in android 4+:

 view.setBackgroundDrawable(0);
查看更多
走好不送
3楼-- · 2019-01-21 08:59

This helped me remove background color, hope it helps someone. setBackgroundColor(Color.TRANSPARENT)

查看更多
男人必须洒脱
4楼-- · 2019-01-21 09:02

In addition to the excellent answers, if you want to achieve this via xml then you can add:

android:background="@android:color/transparent

to your view.

查看更多
▲ chillily
5楼-- · 2019-01-21 09:03

Best performance on this method :

imageview.setBackgroundResource(R.drawable.location_light_green);

Use this.

查看更多
时光不老,我们不散
6楼-- · 2019-01-21 09:04

Use setBackgroundColor(Color.TRANSPARENT) to set the background as transparent, or use setBackgroundColor(0). Here Color.TRANSPARENT is the default attribute from color class. It will work fine.

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-21 09:05

Try this

RelativeLayout relative = (RelativeLayout) findViewById(R.id.widget29);
relative.setBackgroundResource(0);

Check the setBackground functions in the RelativeLayout documentation

查看更多
登录 后发表回答