How to set background color of a View

2019-01-02 20:00发布

I'm trying to set the background color of a View (in this case a Button).

I use this code:

// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();

It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?

Thanks.

14条回答
倾城一夜雪
2楼-- · 2019-01-02 20:22

You made your button transparent. The first byte is the alpha.

Try v.setBackgroundColor(0xFF00FF00);

查看更多
步步皆殇っ
3楼-- · 2019-01-02 20:28

I use at API min 16 , target 23

Button WeekDoneButton = (Button) viewWeeklyTimetable.findViewById(R.id.week_done_button);

WeekDoneButton.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));
查看更多
登录 后发表回答