Setting background colour of Android layout element

后端 未结 9 1900
北海茫月
北海茫月 2020-12-02 03:42

I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.

I have cre

9条回答
  •  北海茫月
    2020-12-02 04:43

    The above answers are nice.You can also go like this programmatically if you want

    First, your layout should have an ID. Add it by writing following +id line in res/layout/*.xml

    
    

    Then, in your Java code, make following changes.

    RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
    rl.setBackgroundColor(Color.RED);
    

    apart from this, if you have the color defined in colors.xml, then also you can do programmatically :

    rl.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
    

提交回复
热议问题