Android programming is driving me crazy. The XML or programmable ways for GUI development is making a right old dogs breakfast of the code--some stuff here, some stuff there.
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. I would like to use a system constant, something like java.awt.SystemColor.control
, but specified in 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}"
.
You can use things like "@color/white", or for android system constants, "@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"
List of resources available for android:color here (like 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:
<?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:
android:background="@color/greenlight"
check out this page: http://android-er.blogspot.com/2010/03/using-color-in-android.html
You can use: "@android:color/white" in your xml. 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.
像这样使用: android:background="#FF00FF"
You don't need using xml file to create a constants' XML,
it can help you: Java constants file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.