簡體   English   中英

如何在xml布局中設置背景顏色?

[英]how to set background color in the xml layout?

我無法找到有關如何在任何地方執行此操作的信息? 我想在活動的xml布局中定義背景顏色。 我該怎么做呢?

采用外部布局(例如LinearLayout )並將其背景屬性設置為顏色。

<LinearLayout android:background="@color/mycolor"
              .... />

這些顏色可以在res/values/colors.xml文件中定義(請參見此處如何執行此操作)

您也可以直接在屬性上定義顏色( android:background="#ffff0000" ),但這通常不好。 通過定義XML文件中的顏色,您可以為其指定一個描述性名稱(提高代碼可讀性),並且可以在其他地方重用它。


編輯:
這是我鏈接的文檔中的一個示例,但這是一個簡短的示例:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ffff0000</color>
    <color name="green">#ff00ff00</color>
</resources>

它基本上是包含多個color標簽的resources標簽。 每種顏色都有一個name屬性(用於引用顏色)和實際顏色。 這是在十六進制的顏色標簽之間定義的。 有關可能的格式,請參閱文檔。 這個是#AARRGGBB ,其中A=alpha (透明度)R=redG=greenB=blue 此示例文件包含完整的紅色和全綠色。 它們可以通過@color/red@color/green引用。

有一點需要注意的是,如果將這個句子應用於include指令,則"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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM