使用shape drawable作为我的背景xml

n17*_*911 23 java android

如果有人可以帮我使用如何使用shape drawable作为我的视图的背景xml,我真的很感激.

这就是我的尝试:但我从未得到过这种颜色.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>
Run Code Online (Sandbox Code Playgroud)

我试过了,不行

<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle"
            android:color="#6000>

</shape>
Run Code Online (Sandbox Code Playgroud)

我试过了,不行

<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle"
            android:background="#6000>
</shape>
Run Code Online (Sandbox Code Playgroud)

我谷歌这是我发现尝试的有限结果.

Luc*_* S. 37

您的颜色设置错误,必须指定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>
Run Code Online (Sandbox Code Playgroud)

  • 目前还不清楚9个人如何将该答案列为所述问题的解决方案!请参阅http://developer.android.com/guide/topics/resources/more-resources.html#Color.不需要32位颜色!此外,我发布了使用24位彩色形状drawables的应用程序.请参阅http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape.最后,我尝试了"已批准"的代码 - 它失败了! (11认同)
  • 道歉 - 我应该说"它不起作用" - 不是"失败!" (2认同)

Ten*_*ous 5

好的 - 我很确定我的问题和驱动你的问题是一样的,而且我找到了它的原因.

问题是冲突的资源定义(特别是资源文件名).比如说,出于某种原因,你在项目的/ res/color /中放了一个名为"drawable_bg.png"的文件; 并忘记你做了这件事(或偶然发生了).如果您尝试在名为"res/drawable/dialog_bg.xml"的项目中定义Shape Drawable,则PNG(来自"MyLib")优先.由于您可以为不同的DPI,form-factor,SDK等提供许多"res"文件夹 - 因此最终可以轻松解决文件名冲突问题.Android库项目也会发生这种情况.如果您的项目对自己拥有资源的项目有任何依赖性,则可能会导致冲突.正如我今天发现的那样,Eclipse可以在许多情况下隐藏或不显示有关此问题的警告.

发生这种情况时,很容易看起来没有应用Shape Drawable.由于"dialog_bg.png"可能不是为您的视图设计的,因此您会得到意想不到的结果,并且很容易对实际发生的事情感到困惑.

解决此问题的最简单方法是重命名项目中的drawable形状.如果问题出在Android库项目中的资源上,那么可以通过应用http://tools.android.com/recent/buildchangesinrevision14中所述的推荐做法找到更好的解决方案.