读书人

修订:更改listView中item不同状态上的

发布时间: 2012-09-10 22:20:13 作者: rapoo

修订:更改listView中item不同状态下的颜色

当更改每个item点击后出现不同的颜色的方法;

<style name="Widget.AbsListView"> <item name="android:listSelector">@drawable/my_selector</item> </style>

?

?my_selector在drawable

<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2008 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@color/transparent" /> <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. --> <item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_disabled" /> <item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_background_disabled" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /> <item android:state_focused="true" android:drawable="@drawable/list_selector_background_focus" /> </selector>

?

?我想在代码中实现

public View getView(final int position, View convertView, ViewGroup parent) { int colorPos = position % colors.length; ... convertView.setBackgroundColor(colors[colorPos]); return convertView; }

?

?

可是没有成功 是不是没有刷新呢 画面呢

?

根据上面都没有成功,我看了好多有的成功有的没有成功不知道怎么弄的 就连第一种也没成功

color.xml

?android:background="@drawable/list_item_style"
就是要变化的背景

?

The items in ListView is TextView, as below:

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/phone_numbers_row_text_tv"
xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:layout_centerHorizontal="true"
? ?android:layout_width="wrap_content"
? ?android:layout_height="wrap_content"
? ?android:background="@drawable/list_view_selector"
? ?android:gravity="center"
? ?/>

The "list_view_selector" is as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
? ? ? ?<item
? ? ? ? ? ? ? ?android:state_selected="true"
? ? ? ? ? ? ? ?android:drawable="@drawable/number_select_bg"/>
? ? ? ?<item
? ? ? ? ? ? ? ?android:state_pressed="true"
? ? ? ? ? ? ? ?android:drawable="@drawable/number_select_bg"/>
? ? ? ?<item
? ? ? ? ? ? ? ?android:state_focused="true"
? ? ? ? ? ? ? ?android:drawable="@drawable/number_select_bg"/>
? ? ? ?<item
? ? ? ? ? ? ? ?android:drawable="@drawable/number_bg"/>
</selector>

读书人网 >移动开发

热点推荐