Selector in Android is one of problems confusing developers. The reason is that it is very hard for developers to control attributes of selector, for ListView and GridView in particular. The paddings of selector, for instance, are very confusing. If you use Android's resources, such as android.R.drawable.gallery_thumb and set it as selector of GridView, the paddings are 16px(in WVGA). These paddings will be adding around the view in GridView but inside the selector, which means the selector will be stretched to contain your view and the paddings. But, if you put the same resources in your own package's resource folder /drawable/my_selector.xml , the images used in selector are the same. Set it as selector for GridView, the paddings are 0 and, what's worse, the selector won't be stretched to contain your view, which means the selector stays in its own size and might be smaller than your view. This is rather wierd and I wish the upcoming SDK release can fix such an issue.

In addition to the difference between Android's resources and developer defined, such paddings of selector are unable to change for developers. Sometimes, our layout is a little bigger which almost reaches screen's edges. In such condition, after adding selector(Android's resource), the selector will stretch out of screen. This huants many developers a lot. It haunts me for a great while at least.

To control the paddings, there are two ways:

1, also is the simpliest one, that is re-design the image resources to exactly fit your view. But this will limit the compatbility which will cause nightmare when maintaining.

2. Another way is implement desired GridView by yourself which enables you open interface to control these paddings when setting selectors. Note: simply inherit GridView or ListView and override method setSelector is not enough, because the fields controlling paddings inside AbsListView are package access control, which prevents classes in other packages including child classes. One referencable example is GridViewSpecial in Andorid's Gallery, which inherits View drectly and implement all it needs by itself.

3. Third way is use shape instead of images to implement desired selector, which enables you to control everything. Let's take a look at an example first:

<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false"> <shape> <stroke android:width="2dip" android:color="#dcdcdc" /> <solid android:color="000000"/> <padding android:left="10dip" android:top="10dip" android:right="10dip" android:bottom="10dip" /> </shape> </item> </selector>

This is a simple selector. For this selector, you can control everything: stroke width, strok color , most important the paddings. There are many other attributes of shape such as gradient, corner enabling you to create more impressive selectors.

Although you can control the selector in more ideally way, it still causes troubles. The selector set to GridView, however, will affect your layout, even if you have set the vertical spacings and horizontal spacings. This needs you should adjust layout after setting selectors. So, implement your own GridView might be the best solution which is much easier to maintain, costly at first much easily after first pain, though.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 用户注册表单以及选择器权重与上下文选择
  2. box-sizing的作用+伪类选择器的参数 an+b
  3. CSS盒模型常用属性,单位,选择器
  4. 大气红色新闻手机端
  5. HTML5 初识
  6. 9.26作业-仿站php.cn手机端
  7. 1.模态框 2.flex布局 3.grid属性
  8. 云服务器的操作系统一般怎么选?选错了可以
  9. HTML伪类、盒子模型学习与应用
  10. Linux-04压缩文件操作