【问题标题】:Changing color of custom listview item on focus更改焦点上自定义列表视图项的颜色
【发布时间】:2011-02-16 10:36:09
【问题描述】:

我创建了一个自定义列表并设法在正常和单击视图中更改子项的颜色。但是当我将它应用到焦点视图时,什么也不会发生,默认背景颜色只会显示文本颜色从白色变为黑色。这是我迄今为止所做的工作所使用的 xml 文件。

列表视图的布局 (listview.xml)

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal" android:padding="18sp"
    android:background="@drawable/backgroundselector">
    <ImageView android:id="@+id/imageView1"
        android:layout_height="fill_parent" android:layout_gravity="center_vertical"
        android:layout_width="wrap_content" />
    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:paddingLeft="10sp" android:textSize="20sp" />
</LinearLayout>

clicked.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ffc61212" />
 </shape>

focussed.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ff26e0d7" />
 </shape>

正常的.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
   <solid android:color="#ff0a4d66" />
 </shape>

最后是 backgroundselector.xml

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

请帮助我了解如何在子项获得焦点时更改列表视图的子项的颜色...

【问题讨论】:

    标签: android listview colors focus


    【解决方案1】:

    我的建议是覆盖ListView.onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) 方法,该方法将在单击项目时调用。

    并且在这个方法中,你可以创建一个新的ArrayAdapter。像这样: ArrayAdapter&lt;String&gt; adpater=new ArrayAdapter&lt;String&gt;(CurrentActivity.this,R.layout.foucs,data); 其中data是一个对象数组,用于为ListView提供数据(一般我们使用String[]或者ArrayList类)。

    然后,致电ListView.setAdapter(adapter) 展示您的新风格。

    希望对您有所帮助。我是个贪婪的人:)

    啊,我找到了一种新的方便的方法。

    ListView.onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) 方法中, 只需调用arg1.setBackgroundColor(newColor); 即可更改项目的颜色。

    【讨论】:

    • 但这是为了当列表视图的子节点被点击时。当子项目被聚焦并且它的颜色会改变时,我期待着一些东西......无论如何感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多