安卓的LinearLayout与颜色资源:我究竟做错了什么? [英] Android LinearLayout with color resource: What am I doing wrong?
问题描述
我跟着本教程创建颜色状态列表为特定的Android的视图。我只想让用户知道为什么屏幕只是改变它突出单击时。
I followed this tutorial to create a color state list for a particular Android view. I just want it to highlight when clicked so the user knows why the screen just changed.
在该视图显示,我得到以下错误:
When the view is rendered, I get the following error:
org.xmlpull.v1.XmlPullParserException:二进制XML文件中的行#3:标签需要一个'绘制'属性或子标签定义绘制
org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: tag requires a 'drawable' attribute or child tag defining a drawable
我的色彩XML(在RES /彩色/ viewcolor.xml):
My color XML (in res/color/viewcolor.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ff33ffff"/> <!-- pressed -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
我的布局XML(在RES /布局/ myview.xml):
My layout XML (in res/layout/myview.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myview"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:background="@color/viewcolor">
<!--crap in the layout-->
</LinearLayout>
我错过了什么?
What did I miss?
推荐答案
我记得我使用状态绘制,而不是国家的色彩工作围绕此错误。出于某种原因,布局的背景只是不带状态的颜色的工作。因此,尝试创建一个有状态的绘制(形状可绘有不同颜色的例子列表),并把它作为背景。
I remember that I worked around this error by using state drawable instead of state color. For some reason layout background just doesn't work with stateful colors. So try creating a stateful drawable (for example list of shape drawables with different colors) and use it as background.
RES /绘制/ pressed.xml:
res/drawable/pressed.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff33ffff" />
</shape>
RES /绘制/ normal.xml:
res/drawable/normal.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ff000000" />
</shape>
RES /绘制/ background.xml:
res/drawable/background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed" />
<item android:drawable="@drawable/normal" />
</selector>
然后用background.xml绘制作为背景:)
Then use background.xml drawable as background :)
这篇关于安卓的LinearLayout与颜色资源:我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!