How to get a Button's background back to defau

2019-02-05 15:07发布

So I'm setting a button's background doing this:

b.setBackgroundResource(R.drawable.custom_button1);

How do I programmatically set it back to the default (boring grey) Android button? Is there a R.android.boring_grey resource identifier I can reference without recreating those states myself? Couldn't seem to find it. Maybe my Googling skills are failing me.

Oh and by the way I tried this:

   b.setBackgroundResource(0);

And the button actually disappeared (blended with black background?).

5条回答
【Aperson】
2楼-- · 2019-02-05 15:35

first get the default background of Button b; using

Drawable d = b.getBackground();

then set another background of your choice

b.setBackgroundResource(R.drawable.custom_button1);

if you need default background again use this

b.setBackgroundDrawable(d);
查看更多
小情绪 Triste *
3楼-- · 2019-02-05 15:37

Have you tried this?

android.R.drawable.btn_default;
查看更多
SAY GOODBYE
4楼-- · 2019-02-05 15:48

try

button.setBackgroundResourses(R.drawable.yourimage);

it will set the default background of buttons. and you can read more default properties of android widgets from given link: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml

查看更多
Luminary・发光体
5楼-- · 2019-02-05 15:52

I did it by

this.setBackgroundResource(android.R.drawable.btn_default);

查看更多
等我变得足够好
6楼-- · 2019-02-05 16:00

Use this

b.tr.setBackgroundDrawable(null);
查看更多
登录 后发表回答