Java 使用形状可绘制作为我的背景 xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1276346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using shape drawable as my background xml
提问by n179911
I really appreciate if someone can help me with using how to use shape drawable as my background xml for my view.
如果有人可以帮助我使用如何使用形状可绘制作为我视图的背景 xml,我真的很感激。
This is what I tried: But I never get the color. Android always gives me black text on white background, regardless what color attribute I put.
这是我尝试过的:但我从来没有得到颜色。Android 总是给我白色背景上的黑色文本,无论我放置什么颜色属性。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dip" android:color="#FBBB" />
<solid android:color="#6000"/>
</shape>
I tried , does not work
我试过了,不行
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:color="#6000>
</shape>
I tried , does not work
我试过了,不行
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:background="#6000>
</shape>
I google this is the limited result I found to try.
我谷歌这是我发现尝试的有限结果。
采纳答案by Lucas S.
You have wrong color settings, you must specify 4 byte colors, eg: #ffff8080
您有错误的颜色设置,您必须指定 4 字节颜色,例如: #ffff8080
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#f0600000"/>
<stroke android:width="3dp" android:color="#ffff8080"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
回答by Tenacious
OK - I'm pretty sure my problem is the same as what drove your question, and that I've found its cause.
好的 - 我很确定我的问题与引发您问题的原因相同,并且我已经找到了原因。
The problem is conflicting resource definitions (specifically, resource filenames). Say, for example, for some reason you put a file named "drawable_bg.png" in /res/color/ in your project; and forgot that you did this (or it happened accidentally). If you then try to define a Shape Drawable in your project named "res/drawable/dialog_bg.xml" - the PNG (from 'MyLib') takes precedence. Since you can have many "res" folders for different DPI, form-factor, SDK, etc - it's fairly easy to wind up with a filename collision. This can also happen with Android Library projects. If your project has any dependencies on projects which themselves have resources, they can cause conflicts. As I just found today, Eclipse can either hide or fail to show a warning about this in many situations.
问题是资源定义冲突(特别是资源文件名)。例如,由于某种原因,您在项目的 /res/color/ 中放置了一个名为“drawable_bg.png”的文件;并忘记了您这样做(或意外发生)。如果您随后尝试在名为“res/drawable/dialog_bg.xml”的项目中定义 Shape Drawable - PNG(来自“MyLib”)优先。由于您可以为不同的 DPI、外形、SDK 等设置许多“res”文件夹 - 很容易导致文件名冲突。这也可能发生在 Android 库项目中。如果您的项目对本身有资源的项目有任何依赖,它们可能会导致冲突。正如我今天刚刚发现的那样,在许多情况下,Eclipse 可以隐藏或不显示有关此的警告。
When this happens it can easily appear that the Shape Drawable is not applied. Since "dialog_bg.png" probably isn't designed for your view you get unexpected results and it's easy to be confused about what's really going on.
发生这种情况时,很容易出现未应用 Shape Drawable 的情况。由于“dialog_bg.png”可能不是为您的视图而设计的,因此您会得到意想不到的结果,并且很容易对真正发生的事情感到困惑。
The easiest way to solve this is to rename the shape drawable in your project. If the problem is with a resource(s) in an Android Library Project, then there may be a better solution found by applying the recommended practice as described at http://tools.android.com/recent/buildchangesinrevision14.
解决此问题的最简单方法是重命名项目中可绘制的形状。如果问题出在 Android 库项目中的资源上,那么通过应用http://tools.android.com/recent/buildchangesinrevision14 中所述的推荐做法,可能会找到更好的解决方案。
回答by Melinda Green
It seems like there are a couple of problems here. The biggest one seems to be the idea that you can use a shape as a text color and that doesn't seem to make sense. You can use a color as a background of a shape, and you can set a shape as the background of view but you can't set a shape as text background or foreground.
这里似乎有几个问题。最大的一个想法似乎是您可以将形状用作文本颜色,但这似乎没有意义。您可以使用颜色作为形状的背景,也可以将形状设置为视图的背景,但不能将形状设置为文本背景或前景。
The other thing that looks wrong is that in the alternative XML files that you tried, the end quotes around the color values are missing, so that shouldn't compile at all.
另一件看起来不对的事情是,在您尝试的替代 XML 文件中,缺少颜色值周围的结尾引号,因此根本不应该编译。
回答by Muhammad Aamir Ali
You are giving a wrong hexa color attribue. It should be eight digits after #. e.g #00000000
您给出了错误的六色属性。#后面应该是八位数字。例如#00000000
回答by Faxriddin Abdullayev
try this
尝试这个
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:radius="0.1dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<solid android:color="#Efffff" />
<stroke
android:width="2dp"
android:color="#25aaff" />
</shape>