Setting background colour of Android layout element

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

    You can use simple color resources, specified usually inside res/values/colors.xml.

    <color name="red">#ffff0000</color>
    

    and use this via android:background="@color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(R.color.red).

    You can also use any drawable resource as a background, use android:background="@drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).

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

    If you want to change a color quickly (and you don't have Hex numbers memorized) android has a few preset colors you can access like this:

    android:background="@android:color/black"
    

    There are 15 colors you can choose from which is nice for testing things out quickly, and you don't need to set up additional files.

    Setting up a values/colors.xml file and using straight Hex like explained above will still work.

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

    The answers above all are static. I thought I would provide a dynamic answer. The two files that will need to be in sync are the relative foo.xml with the layout and activity_bar.java which corresponds to the Java class corresponding to this R.layout.foo.

    In foo.xml set an id for the entire layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/foo" .../>
    

    And in activity_bar.java set the color in the onCreate():

    public class activity_bar extends AppCompatActivty {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.foo);
    
                //Set an id to the layout
            RelativeLayout currentLayout = 
                        (RelativeLayout) findViewById(R.id.foo);
    
            currentLayout.setBackgroundColor(Color.RED);
            ...
        }
        ...
    }
    

    I hope this helps.

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

    Kotlin

    linearLayout.setBackgroundColor(Color.rgb(0xf4,0x43,0x36))
    

    or

    <color name="newColor">#f44336</color>
    

    -

    linearLayout.setBackgroundColor(ContextCompat.getColor(vista.context, R.color.newColor))
    
    0 讨论(0)
  • 2020-12-02 04:37

    4 possible ways, use one you need.

    1. Kotlin

    val ll = findViewById<LinearLayout>(R.id.your_layout_id)
    ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
    

    2. Data Binding

    <LinearLayout
        android:background="@{@color/white}"
    

    OR more useful statement-

    <LinearLayout
        android:background="@{model.colorResId}"
    

    3. XML

    <LinearLayout
        android:background="#FFFFFF"
    
    <LinearLayout
        android:background="@color/white"
    

    4. Java

    LinearLayout ll = (LinearLayout) findViewById(R.id.your_layout_id);
    ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
    
    0 讨论(0)
  • 2020-12-02 04:38

    The

    res/values/colors.xml.
    
    <color name="red">#ffff0000</color>
    android:background="@color/red"
    

    example didn't work for me, but the

    android:background="#(hexidecimal here without these parenthesis)"
    

    worked for me in the relative layout element as an attribute.

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