设置Android布局元素的背景色 [英] Setting background colour of Android layout element

查看:50
本文介绍了设置Android布局元素的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Android 上的一组幻灯片中克隆一个活动的设计,通常在res/values/colors.xml中指定.

#ffff0000</color>

并通过 android:background="@color/red" 使用它.这种颜色也可以用于其他任何地方,例如作为文本颜色.以同样的方式在 XML 中引用它,或者通过 getResources().getColor(R.color.red) 在代码中获取它.

您还可以使用任何可绘制资源作为背景,为此使用 android:background="@drawable/mydrawable"(这意味着 9patch drawables、普通位图、shape drawables,..).

I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.

I have created the layout as shown in the image, and the header is a TextView in a RelativeLayout. Now I wish to change the background colour of the RelativeLayout, however I cannot seem to figure out how.

I know I can set the android:background property in the RelativeLayout tag in the XML file, but what do I set it to? I want to define a new colour that I can use in multiple places. Is it a drawable or a string?

Additionally I would expect there to be a very simple way to this from within the Eclipse Android UI designer that I must be missing?

I am a bit frustrated currently, as this should be an activity that is performed with a few clicks at maximum. So any help is very appreciated. :)

解决方案

You can use simple color resources, specified usually inside res/values/colors.xml.

<color name="red">#ffff0000</color>

and use this via android:background="@color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(R.color.red).

You can also use any drawable resource as a background, use android:background="@drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).

这篇关于设置Android布局元素的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆