2014-03-24 37 views
0

编辑我做了建议的更改,但仍然收到相同的错误,如下所示。Android:在xml中声明颜色

我有一个布局,我试图设置背景颜色动态响应点击等。

这是布局的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_pare" 
    android:gravity="center_vertical" 
    android:background="@drawable/custom_style" 
    > 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="TextView"/> 
</RelativeLayout> 

而这正是我想声明的颜色,存储在可绘制/ custom_style.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:color="#ff0000" android:state_selected="true"/> 
    <item android:color="#ffffff" android:state_pressed="true" /> 
    <item android:color="#e3e3e3"/> 
</selector> 

问题是当我尝试运行此应用程序崩溃,出现以下错误:

03-24 16:29:50.540: E/AndroidRuntime(17643): FATAL EXCEPTION: main 
03-24 16:29:50.540: E/AndroidRuntime(17643): android.view.InflateException: Binary XML     file line #7: Error inflating class <unknown> 
03-24 16:29:50.540: E/AndroidRuntime(17643): at android.view.LayoutInflater.createView(LayoutInflater.java:626) 
03-24 16:29:50.540: E/AndroidRuntime(17643): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)... 
+0

看到我的答案有错误,当你声明你的颜色。 – Fllo

+1

在你的XML中,你已经设置了layout_height =“fill_pare”,如果你把fill_parent(或者更好的match_parent)仍然存在错误?如果错误仍然存​​在,您可以发布崩溃活动的onCreate方法的代码? – jaumebd

+0

@jaumebd我认为你只是找到解决方案! ^^ – Fllo

回答

3

这不是color绘制(.XML),它应该是:

android:background="@drawable/custom_style" 

你应该把你的custom_style.xmldrawable文件夹:
(右键单击文件夹res并创建一个名为drawable的文件夹)

enter image description here

还改变你的selector如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:drawable="@color/color_one" android:state_selected="true"/> 
    <item android:drawable="@color/color_two" android:state_pressed="true" /> 
    <item android:drawable="@color/color_three"/> 
</selector> 

进入/ RES /值创建一个名为colors.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="color_one" >#ff0000</color> 
    <color name="color_two" >#ffffff</color> 
    <color name="color_three" >#e3e3e3</color> 
</resources> 

最后,@color/color_name引用在您的项目的选择你的颜色。

从参考:

Note: A color resource can also be used as a drawable in XML. For example, when creating a state list drawable, you can reference a color resource for the android:drawable attribute (android:drawable="@color/green").


由于@jaumebd(鹰眼 ^^)说,你的问题是这条线android:layout_height="fill_pare",这是android:layout_height="fill_parent"

这应该工作。

+0

第二个XML缩减了我五个“颜色类型不允许”错误。 – spacitron

+0

太棒了,终于id不会崩溃。尽管如此,仍然没有做我想做的事。 – spacitron

+0

啊..很高兴阅读!祝你好运。 – Fllo

0

变化

android:background="@color/custom_style" 

android:background="@drawable/custom_style"` 

和地点custom_style.xml文件夹 “绘制” 在

+0

那么我得到完全相同的错误。 – spacitron

+0

custom_style在可绘制的文件夹(不是值)? – MichelRobico

+0

是的。它给了我同样的错误。这就是为什么我把它移到颜色(对不起,不是值)的原因。 – spacitron

0

你应该把custom_style.xml到您绘制的文件夹。

更改以下在XML文件中的代码:

android:background="@color/custom_style" 

android:background="@drawable/custom_style" 
0

首先把你的custom_style在res /可绘制文件夹(如果不是在你的项目中存在,创建它) 。

在您的代码中,您尝试使用可绘制的xml作为颜色,这是错误。

要被拉伸设置为背景正确,您必须使用:

android:background="@drawable/custom_style" 
0

custom_style.xml应在绘制文件夹。因此,您的布局XML文件将更改为

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_pare" 
    android:gravity="center_vertical" 
    android:background="@drawable/custom_style" 
    > 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="TextView"/> 
</RelativeLayout>