如何在xml布局中设置背景颜色?
我无法在任何地方找到有关如何执行此操作的信息?我想在活动的 xml 布局中定义背景色。我该怎么做?
采用外部布局(例如 a ),并将其背景属性设置为颜色。LinearLayout
<LinearLayout android:background="@color/mycolor"
.... />
这些颜色可以在文件中定义(请参阅此处如何执行此操作)。res/values/colors.xml
您也可以直接在属性 () 处定义颜色,但这通常并不好。通过定义 XML 文件中的颜色,您可以为其指定一个描述性名称(提高代码可读性),并且可以在其他位置重用它。android:background="#ffff0000"
编辑:
在我链接的文档中有一个示例,但这里有一个简短的示例:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ffff0000</color>
<color name="green">#ff00ff00</color>
</resources>
它基本上是一个包含多个标签的标签。每种颜色都有一个属性(用于引用颜色)和一个实际颜色。这是在十六进制的颜色标签之间定义的。请参阅文档,了解可能的格式。这个是 ,其中(透明度)、 和 。此示例文件包含全红色和全绿色。它们可以通过 和 引用。resources
color
name
#AARRGGBB
A=alpha
R=red
G=green
B=blue
@color/red
@color/green
有一件事需要提到,如果将这句话应用于inclus指令,则该设置不起作用。"android:background="#ffffffff""
例如
<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" />
应在module_printer_detail_property的布局文件中设置。"android:background"