带有颜色资源的 Android LinearLayout:我做错了什么? [英] Android LinearLayout with color resource: What am I doing wrong?

查看:19
本文介绍了带有颜色资源的 Android LinearLayout:我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照本教程创建了一种颜色特定 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 行:标签需要drawable"属性或定义可绘制对象的子标签

org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: tag requires a 'drawable' attribute or child tag defining a drawable

我的颜色 XML(在 res/color/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/layout/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>

我错过了什么?

推荐答案

我记得我通过使用 state drawable 而不是 state color 解决了这个错误.出于某种原因,布局背景不适用于有状态的颜色.所以尝试创建一个有状态的可绘制对象(例如具有不同颜色的形状可绘制对象列表)并将其用作背景.

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/drawable/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/drawable/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/drawable/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 drawable 作为背景 :)

Then use background.xml drawable as background :)

这篇关于带有颜色资源的 Android LinearLayout:我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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