android:background="" is the attribute used to set background for any Layout file. You can directly specify the value as HEX color code as we do for CSS files in HTML. You can also add transparency to the color by adding 2 more hex numbers after the # (hash) symbol.
it would be very easy, but bad practice :) It is even easier to use android:background="@android:color/white" which is predefined and doesn't require you to add anything to strings. xml.
Take your outer layout (e.g. a LinearLayout
) and set its background attribute to a color.
<LinearLayout android:background="@color/mycolor"
.... />
These colors can be defined in the res/values/colors.xml
file (see here how to do this).
You can also define a color directly at the attribute (android:background="#ffff0000"
), but that's usually not good. By defining the colors in the XML file you can give it a descriptive name (improves code readability) and you can reuse it somewhere else.
Edit:
Theres an example in the doc I linked, but here is a short example how it looks:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ffff0000</color>
<color name="green">#ff00ff00</color>
</resources>
Its basically a resources
tag containing multiple color
tags. Each color has a name
attribute (which you use to reference the color) and an actual color. That is defined between the color tags in hex. See the docs for possible formats. This one is #AARRGGBB
, where A=alpha
(transparency), R=red
, G=green
and B=blue
. This example file contains a full red and a full green color. They can be referenced via @color/red
and @color/green
.
there is one thing need to mention is that the "android:background="#ffffffff""
setting does not work if this sentence is applied to an include directive.
for example,
<include
android:id="@+id/fragment_printer_detail_property_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/printer_detail_group_vertical_margin"
android:layout_marginLeft="@dimen/printer_detail_group_horizontal_margin"
android:layout_marginRight="@dimen/printer_detail_group_horizontal_margin"
android:layout_marginTop="@dimen/printer_detail_group_vertical_margin"
layout="@layout/module_printer_detail_property"
android:background="@color/module_printer_detail_group_background_color" />
the "android:background"
should be set in the layout file of module_printer_detail_property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With