Setting background colour of Android layout element

后端 未结 9 1843
北海茫月
北海茫月 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

    <RelativeLayout ...
    ...
    android:id="@+id/your_layout_id"
    ...
    </RelativeLayout>
    

    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));
    
    0 讨论(0)
  • 2020-12-02 04:45

    You can use android:background="#DC143C", or any other RGB values for your color. I have no problem using it this way, as stated here

    0 讨论(0)
  • 2020-12-02 04:48

    Android studio 2.1.2 (or possibly earlier) will let you pick from a color wheel:

    I got this by adding the following to my layout:

    android:background="#FFFFFF"
    

    Then I clicked on the FFFFFF color and clicked on the lightbulb that appeared.

    0 讨论(0)
提交回复
热议问题