你是不是也像我一样碰到这样的问题呢?现在我就带着大家一起来解决,我也是通过网上一些资料和自已调试源码得到的答案。
?? 现在我给大家说下我的跟踪步骤:
?? ? ? ? ? ? 1)在网上找到资料说默认的选中效果是也个drawable目录在源码的:frameworks/base/core/res/res/drawable/list_selector_background.xml
?? ? ? ? ? ? ? ?我们要找的就是这个文件,在源码中搜索list_selector_background看在什么地方有做引用。
?? ? ? ? ? ? 2)搜索的结果有好几像,我们选择/android/widget/AbsListView.java:779
?
private void useDefaultSelector() { setSelector(getResources().getDrawable( com.android.internal.R.drawable.list_selector_background)); }
?? ? ? ? ? ? ? ?上面的代码可以看出设置drawable是调用的SetSelector,既然ListView继承AbsListView而且又是public那么ListView也就会有这个方法
?? ? ? ? ? ? ?3)找到了方法,我们就可以仿照list_selector_background.xml写一个xml文件
?? ? ? ? ? ? ? ? ??<?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>
?
???需要注意的是这里的android:drawable要设置的是drawable而不是Color,你可以试下看填Color的效果,本篇附上源码,解决了Color这个问题。