带有颜色资源的 Android LinearLayout:我做错了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3506319/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:31:08  来源:igfitidea点击:

Android LinearLayout with color resource: What am I doing wrong?

androidandroid-layout

提问by iandisme

I followed this tutorialto 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.

我按照本教程为特定的 Android 视图创建了一个颜色状态列表。我只是希望它在点击时突出显示,以便用户知道屏幕刚刚改变的原因。

When the view is rendered, I get the following error:

呈现视图时,出现以下错误:

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

org.xmlpull.v1.XmlPullParserException:二进制 XML 文件第 3 行:标签需要“drawable”属性或定义可绘制对象的子标签

My color XML (in res/color/viewcolor.xml):

我的颜色 XML(在 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>

My layout XML (in res/layout/myview.xml):

我的布局 XML(在 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?

我错过了什么?

回答by Konstantin Burov

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.

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

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>

Then use background.xml drawable as background :)

然后使用 background.xml drawable 作为背景 :)

回答by Austyn Mahoney

Instead of using shapes in your drawable, you can use the android:drawableattribute which accepts a color resource (e.g. @color/black).

您可以使用android:drawable接受颜色资源的属性(例如@color/black),而不是在 drawable 中使用形状。

layout.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="@drawable/myDrawable">
    <!-- other views in layout-->
</LinearLayout>

my_drawable.xml

my_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- focused -->
    <item android:state_focused="true" android:drawable="@color/YOUR_COLOR_HERE" />
    <!-- focused and pressed-->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/YOUR_COLOR_HERE" />
    <!-- pressed -->
    <item android:state_pressed="true" android:drawable="@color/YOUR_COLOR_HERE" />
    <!-- default -->
    <item android:drawable="@color/YOUR_COLOR_HERE" /> 
</selector>

In my_drawable.xmlyou need to make sure that the colors you specify are defined in res/values/colors.xml, or this won't work.

my_drawable.xml您需要确保您指定的颜色在 中定义res/values/colors.xml,否则这将不起作用。

If you want to use an image instead of a color change from a color resource to a drawable resource. Example:

如果要使用图像而不是颜色从颜色资源更改为可绘制资源。例子:

android:drawable="@color/YOUR_COLOR_HERE"
android:drawable="@drawable/YOUR_IMAGE_HERE"