代码之家  ›  专栏  ›  技术社区  ›  iandisme

Android LinearLayout with color resource:我做错了什么?

  •  34
  • iandisme  · 技术社区  · 14 年前

    我跟着 this tutorial 为特定Android视图创建颜色状态列表。我只想它突出显示时,点击,让用户知道为什么屏幕刚刚改变。

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

    我的颜色XML(分辨率/颜色)/视图颜色.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文件):

    <?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>
    

    2 回复  |  直到 14 年前
        1
  •  51
  •   Artem Russakovskii    14 年前

    我记得我通过使用state drawable而不是state color解决了这个错误。由于某些原因,布局背景不能与有状态的颜色配合使用。因此,尝试创建一个有状态的可绘制文件(例如,具有不同颜色的形状可绘制文件列表)并将其用作背景。

    分辨率/可绘制/pressed.xml:

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

    分辨率/可绘制/普通.xml:

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

    分辨率/可绘制/背景.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>
    

        2
  •  49
  •   Austyn Mahoney Janak    13 年前

    您可以使用 android:drawable 接受颜色资源的属性(例如@color/black)。

    <?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>
    

    我的_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>
    

    my_drawable.xml res/values/colors.xml ,否则这行不通。

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

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