已经好久没正儿八经的学习工作了,刚刚换了新工作,虽然不是非常理想,但总算可以踏实下来了,抽出时间总结一下自己的Android知识,废话不多说,进正题。最近遇到一个问题,但具体原因不明确,在这儿写出来,希望了解其中原因的给出具体明确的答案。
相信很多朋友都遇到过自己写的GridView点击某个Item时,发现按下效果很难看,视乎按下产生的阴影超出本身的范围,而且GridView莫名其妙的不能填充整个布局,就像下面这样:
就像图中看到的那样,gridView设置的是填充父容器,但会出现边缘留边,按下效果也超出自身大小范围。
就像大家熟悉的ListView一样,按下效果自然也是selector的作用了,下面我就尝试着去看看GridView的源码:
public class GridView extends AbsListView { .... }
GridView与ListView一样,都是继承自AbsListView,对selector的设置都在父类中实现:
Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mSelector == null) { useDefaultSelector(); } ..... //在onMeasure()方法中,对 //selector进行判断,如果没有通过代码进行设置,也没有在布局文件中设置, //则去调用useDefaultSelector();方法 }
private void useDefaultSelector() { setSelector(getResources().getDrawable( com.android.internal.R.drawable.list_selector_background)); //在这里我发现默认回去调用系统给出的selector,顺藤摸瓜我们来找到这个文件 }
<?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:drawable="@color/transparent" android:state_window_focused="false"/> <!-- 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:drawable="@drawable/list_selector_background_disabled" android:state_enabled="false" android:state_focused="true" android:state_pressed="true"/> <item android:drawable="@drawable/list_selector_background_disabled" android:state_enabled="false" android:state_focused="true"/> <item android:drawable="@drawable/list_selector_background_transition" android:state_focused="true" android:state_pressed="true"/> <item android:drawable="@drawable/list_selector_background_transition" android:state_focused="false" android:state_pressed="true"/> <item android:drawable="@drawable/list_selector_background_focus" android:state_focused="true"/> </selector>
然后找到对应的图片和transition文件:
list_selector_background_disabled 是.9.png文件
list_selector_background_focus
下面是 list_selector_background_transition
<?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. --> <transition xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:drawable/list_selector_background_pressed" /> <item android:drawable="@android:drawable/list_selector_background_longpress" /> </transition>
press对应图片与focus相近,到这里,我们发现默认的图片都是.9.png图片,那么是不是由于这个原因呢?试一下就知道了。
将.9.png换为普通png,发现效果成了这样:
GridView铺满了父容器,点击Item没有反应,证明可能selector可能设置成功,按下效果大小与自身相同,但由于我Item设置了背景颜色所以看不到。
这就是效果了,平时我们可能更多的是把自定义selector做为Item的背景,gridview随便设置一个自定义selector就可以了,但要记住不要用.9.png图片。
就写这些吧,可能理解有错误的地方,请批评指正。
为什么.9.png图片会造成这种效果? 还希望能有大牛能指点迷津。