Android开发中view状态变化样式篇
来源:互联网 发布:电信测速软件下载 编辑:程序博客网 时间:2024/06/05 07:19
1 首先是 定义 item的 selector \drawable\listview_item_pressed.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:state_pressed="true"
- android:drawable="@drawable/listview_item_selected_bg" />
- <item
- android:state_pressed="false"
- android:drawable="@drawable/listview_unseleceted" />
- </selector>
2 把selector 添加入 item ,这里采用的是 添加进 item 采用的布局文件中
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:background="@drawable/listview_item_pressed"
- android:orientation="horizontal"
- android:layout_height="56dp">...
3 在 适配器中 getView 方法里 添加 点击事件,也可以在 listView 中的 onItemClick()方法中处理
- convertView.setTag(position);
- convertView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(oldView == null){
- //第一次点击
- oldView = v;
- v.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.listview_item_selected_bg));
- }else{
- //非第一次点击
- //把上一次点击的 变化
- oldView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.list_pressed));
- v.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.listview_item_selected_bg));
- //保存oldView
- oldView = v;
- }
- }
- });
- if(oldView != null && (position == (Integer)oldView.getTag())){// 为点击 item
- convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.listview_item_selected_bg));
- }else{
- convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.list_pressed));
- }
============================
1
2
3
<
Button
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
1
android:background="@drawable/upload"
1
2
3
4
<
ImageButton
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:src
=
"@drawable/upload"
/>
1
2
3
4
5
6
7
8
9
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
selector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
item
android:state_pressed
=
"true"
android:drawable
=
"@drawable/up_pressed"
/>
<
item
android:state_pressed
=
"false"
android:drawable
=
"@drawable/up"
/>
</
selector
>
1
android:background="@drawable/button_selector"
1
2
3
4
5
6
7
8
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
shape
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:shape
=
"rectangle"
>
<
gradient
android:startColor
=
"#808080"
android:endColor
=
"#808080"
android:angle
=
"-90"
/>
</
shape
>
1
2
3
4
5
6
7
8
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
shape
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:shape
=
"rectangle"
>
<
gradient
android:startColor
=
"#FF7F00"
android:endColor
=
"#EE7600"
android:angle
=
"-90"
/>
</
shape
>
1
2
3
4
5
6
7
8
9
10
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
selector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
item
android:state_pressed
=
"true"
android:drawable
=
"@drawable/shape_pressed"
/>
<
item
android:state_pressed
=
"false"
android:drawable
=
"@drawable/shape_normal"
/>
</
selector
>
1
2
3
4
5
6
7
<
ImageButton
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:src
=
"@drawable/upload_pressed"
android:padding
=
"5dp"
android:layout_centerVertical
=
"true"
android:background
=
"@drawable/button_selector"
/>
0 0
- Android开发中view状态变化样式篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- Android样式的开发:View Animation篇
- ANDROID样式的开发:VIEW ANIMATION篇
- 判断andorid开发中网络状态变化
- Android 中监听WIFI连接状态变化
- 【转载】Android样式的开发:View Animation篇
- Android样式的开发(五):View Animation篇
- 动态代理简单实例
- kinect+openni手势识别
- NYOJ 463 九九乘法表
- 在一个js文件中引入另一个js文件
- 下拉列表属性
- Android开发中view状态变化样式篇
- 黑马程序员:初步接触try catch异常捕获机制
- Cocos2d-X游戏【泰然网《跑酷》】JS到C++移植10:Adding Coins and Rocks【添加金币和障碍物】
- 编辑字符串距离
- C++中堆和栈的区别
- 2013计算机视觉代码合集二
- Chapter 13 Metatables and Metamethods
- 检测远程端口是否打开
- iOS开发中那些高效常用的宏