【问题标题】:why does Listview + Selector crash from xml file为什么Listview + Selector会从xml文件中崩溃
【发布时间】:2011-02-09 11:53:54
【问题描述】:

我有一个列表视图,我希望背景在用户点击(触摸)时改变颜色。

每个 listItem 都是休闲的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row_item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/row_background">


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/RowImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight = "1" 
            android:background="#ffffff" 
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:minHeight="48dp"
            android:maxHeight="48dp"    />

            <TextView android:id="@+id/RowText1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingTop="16dp"                   
                android:textSize="18sp"
                android:textStyle="bold"
                android:textColor="#000080"
                android:background="#ffffff" 
                android:layout_weight = "2"  />
    </LinearLayout>
</LinearLayout>

在drawable里面我有一个闲置文件(row_background.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:background="@color/white" />
    <item android:state_focused="true" android:state_pressed="true" android:background="@color/green" />
    <item android:state_pressed="true" android:background="@color/green" />
    <item android:background="@color/white" />
</selector>

然后我在 values 文件夹 (color.xml) 中有颜色文件:

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <color name="green">#808080</color>
    <color name="white">#ffffff</color>
</resources>

当我尝试使用颜色运行时,选择器会崩溃。如果我只使用白色背景运行,而不调用选择器,一切正常。

谁能告诉我怎么回事?

谢谢, 阿德里安。

【问题讨论】:

    标签: xml listview crash selector


    【解决方案1】:

    XML 中的颜色代码错误...您需要在代码前添加“#FF”...

    <?xml version="1.0" encoding="utf-8"?>
    
        <resources>
        <color name="green">#FF808080</color>
        <color name="white">#FFFFFFFF</color>
        </resources>
    

    【讨论】:

    • 您能解释一下为什么您认为这是解决方案吗?
    猜你喜欢
    • 2017-03-14
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多