技术vs艺术

技术是扎实的基本功,艺术是丰富的想象力;技术的突破总是从夸张的想像中开始的。

博客园 首页 新随笔 联系 订阅 管理

http://www.eoeandroid.com/thread-98564-1-1.html

本帖最后由 taotao_think 于 2011-9-7 16:54 编辑

  先上一张图给大家炫耀下:
<IGNORE_JS_OP style="WORD-WRAP: break-word">ListView无边角 
这张ListView的图中屏蔽了滚动条
使用android:scrollbars="none"
在listView设置背景后,拖动时会出现变黑现象
使用此属性android:scrollingCache="false"
ListView拖动时上下和其他控件接触的地方显示黑色阴影
使用android:fadingEdge="none"
设置每个Item之间的间距用android:dividerHeight="3dip"
设置间距后,系统默认间距是用黑色填充的
使用android:divider="#F5F5F5"设置自定义颜色,也可以设置为图片

接下来是设置item的样式
这个就需要在定义的item的layout文件中设置它的背景了
我用的是<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/wb_list_item_bg">
<TextView
  android:id="@+id/info_title_text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="15dip"
  android:textColor="@color/blue_black"
  android:textStyle="bold" />
<TextView
  android:id="@+id/info_time_text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  style="@style/gz_count"
  android:layout_alignBottom="@id/info_title_text"
  android:layout_alignParentRight="true" />
<TextView
  android:id="@+id/info_content_text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/info_title_text"
  android:textSize="15dip"
  android:textColor="@color/blue_black" />
</RelativeLayout>

注意红色标注的地方就是设置的背景样式,此背景是用XML文件编写的,
利用了Android的selector方法来设置控件在不同状态下的背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="@drawable/wb_selector_item" />
<item android:state_focused="true" android:state_pressed="true"
  android:drawable="@drawable/wb_selector_item_dw" />
<item android:state_focused="false" android:state_pressed="true"
  android:drawable="@drawable/wb_selector_item_dw" />
<item android:state_selected="true" android:drawable="@drawable/wb_selector_item_dw" />
<item android:state_focused="true" android:drawable="@drawable/wb_selector_item_dw" />
</selector>
注意:其中的各item就是Listview中的每行在选中,获取焦点,被点选,无焦点时的背景
其实总共就分两类,一种是在活动状态,那么就给加底色高亮显示也可以是图片;一种是不活动状态,默认按浅颜色显示也可以是图片。

我是用的给控件设颜色和形状来完成不同背景显示的,代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 控件圆角属性,控件默认是直角,给四个顶点分别设置角度值,可以修改为圆角 -->
<corners android:topLeftRadius="10px" android:topRightRadius="10px"
  android:bottomLeftRadius="10px" android:bottomRightRadius="10px" />
<!-- 设置控件的背景颜色 -->
<solid android:color="#F89E4F" />
<!-- 控件边框颜色和粗细 -->
<stroke android:width="1dp" android:color="#CFCFCF" />
<!-- 控件内容据边框间距 -->
<padding android:left="3sp" android:top="3sp" android:right="3sp"
  android:bottom="3sp" />
</shape>

自我感觉这中设置控件显示属性的方式特别有意思,比直接设置背景图片强多了。

设置了背景后有一个问题出现了,就是会在每行四个角显示系统默认的活动状态背景色,很让人不爽。
<IGNORE_JS_OP style="WORD-WRAP: break-word">ListView有边角 

经过摸索,还是找到了解决方法,在ListView控件中使用android:listSelector="#F5F5F5"属性将系统默认的活动状态背景色设为和我们设置android:divider="#F5F5F5"行间距显示相同颜色就可以去掉边角的背景色了。

posted on 2013-05-24 10:28  邓春光  阅读(501)  评论(0编辑  收藏  举报