简体   繁体   English

Android系统颜色常量

[英]Android System Color Constants

Android programming is driving me crazy. Android编程让我发疯。 The XML or programmable ways for GUI development is making a right old dogs breakfast of the code--some stuff here, some stuff there. 用于GUI开发的XML或可编程方式正在制作正确的老狗早餐代码 - 这里有些东西,有些东西。

My current frustration is trying to keep it all XML and I would like to set the background color of the TextView the same color as the background color of a Button without having to specify the exact color code. 我目前的挫折是试图保持所有XML,我想将TextView的背景颜色设置为与Button的背景颜色相同的颜色,而不必指定确切的颜色代码。 I would like to use a system constant, something like java.awt.SystemColor.control , but specified in XML. 我想使用系统常量,如java.awt.SystemColor.control ,但在XML中指定。

Is there anyway of doing this without having to respecify a bunch of stuff? 无论如何这样做而不必重新指定一堆东西? I'm looking for a solution like: android:background="{Insert constant here}" . 我正在寻找一个解决方案,如: android:background="{Insert constant here}"

You can use things like "@color/white", or for android system constants, "@android:color/holo_red_light". 您可以使用“@ color / white”或android系统常量“@android:color / holo_red_light”等内容。 Alternatively if you want more control, you can always define a common background in a drawable and set the background to "@drawable/mydrawable" 或者,如果您想要更多控制,您可以始终在drawable中定义公共背景并将背景设置为“@ drawable / mydrawable”

List of resources available for android:color here (like background_dark): http://developer.android.com/reference/android/R.color.html 可用于android的资源列表:这里的颜色(如background_dark): http//developer.android.com/reference/android/R.color.html

You should have a file in res/values called colors.xml, it should look like this: 你应该在res / values中有一个名为colors.xml的文件,它应该如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#FFFFFF</color>
    <color name="grey">#7A7A7A</color>
    <color name="background">#0044AA</color>
    <color name="bluelight">#449DEF</color>
    <color name="bluedark">#2F6699</color>
    <color name="greenlight">#70C656</color>
    <color name="greendark">#53933F</color>
    <color name="orangelight">#F3AE1B</color>
    <color name="orangedark">#BB6008</color>
    <color name="black">#000000</color>
</resources>

Then you can just call the color in xml like this: 然后你可以像这样在xml中调用颜色:

android:background="@color/greenlight"

check out this page: http://android-er.blogspot.com/2010/03/using-color-in-android.html 看看这个页面: http//android-er.blogspot.com/2010/03/using-color-in-android.html

You can use: "@android:color/white" in your xml. 您可以在xml中使用:“@ android:color / white”。 I imagine that a few other common colors are supported, but I don't know for sure which ones. 我想可以支持其他一些常见的颜色,但我不确定哪些颜色。

The preferred solution is generally to create your own colors.xml resource file and reference the ones that you put inside there. 首选解决方案通常是创建自己的colors.xml资源文件,并引用您放在那里的那些文件。

像这样使用: android:background="#FF00FF"

You don't need using xml file to create a constants' XML, 您不需要使用xml文件来创建常量的XML,

it can help you: Java constants file 它可以帮助你: Java常量文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM