- 浏览: 84490 次
- 性别:
- 来自: 成都
最新评论
原址:http://ssd910.blog.163.com/blog/static/23876797201021610282142/
Button 有按下效果
[功能]
让Button 有按下效果 更有视觉效果
[代码]
1. 先准备2张*.png 一张供默认使用 另一张供按下使用 本例为:
Java代码
- play.png
- play_down.png
play.pngplay_down.png
2. 根据各种状态 定制化所显示的 *.png 命名为: myselection.xml
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:state_pressed="false"
- android:drawable="@drawable/play" />
- <item
- android:state_pressed="true"
- android:drawable="@drawable/play_down" />
- <item
- android:drawable="@drawable/play" />
- </selector>
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false" android:drawable="@drawable/play" /> <item android:state_pressed="true" android:drawable="@drawable/play_down" /> <item android:drawable="@drawable/play" /></selector>
3. 在 main.xml 布局中 添加Button 元件 并 设置 使用 myselection.xml
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Button Style!"
- />
- <ImageButton
- android:id="@+id/playorpause"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@xml/myselection"
- android:background="#00000000" />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button Style!" /><ImageButton android:id="@+id/playorpause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@xml/myselection" android:background="#00000000" /></LinearLayout>
4. 大家可以自己看看效果 因为不好截图
其实 除了上面的方法 还有一个方法 为:
1. 在 maun.xml 中添加 ImageButton 且不设置使用的*.png
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ImageButton
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><ImageButton android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
2. 在该ImageButton上设置监听器 并根据其状态使用对应的资源 但是必须要设置默认资源
Java代码
- ImageButton btn = (ImageButton) findViewById(R.id.button);
- //to set its default *.png
- btn.setBackgroundResource(R.drawable.play);
- btn.setOnTouchListener(new ImageButton.OnTouchListener(){
- @Override
- public boolean onTouch(View arg0, MotionEvent arg1) {
- // TODO Auto-generated method stub
- if(arg1.getAction() == MotionEvent.ACTION_DOWN){
- arg0.setBackgroundResource(R.drawable.play_down);
- }
- else if(arg1.getAction() == MotionEvent.ACTION_UP){
- arg0.setBackgroundResource(R.drawable.play);
- }
- return false;
- }
- });
ImageButton btn = (ImageButton) findViewById(R.id.button); //to set its default *.png btn.setBackgroundResource(R.drawable.play); btn.setOnTouchListener(new ImageButton.OnTouchListener(){ @Override public boolean onTouch(View arg0, MotionEvent arg1) { // TODO Auto-generated method stub if(arg1.getAction() == MotionEvent.ACTION_DOWN){ arg0.setBackgroundResource(R.drawable.play_down); } else if(arg1.getAction() == MotionEvent.ACTION_UP){ arg0.setBackgroundResource(R.drawable.play); } return false; } });
具体哪个方法更好 应该根据自己的场合:
1. 只有一个Button 推荐使用第一个方法
2. 有几个Button 推荐使用第二个 统一定义 然后根据指定的id 来使用目标*.png
发表评论
-
转 android背景选择器selector用法汇总
2011-04-27 15:23 1319原址:http://blog.sina.com.cn/s/bl ... -
(转)编写Android自定义按钮
2011-04-27 11:07 1181原址:http://marshal.easymorse.com ... -
(转)消息机制细谈(message handler looper MessageQueue )
2011-04-12 11:20 1029在handler的简单用 ... -
解决:sqlite数据库含有\n的值或者sax解析xml有\n的值付给TextView,显示却不会换行
2011-03-17 23:00 2907遇到个问题,网上也有人问 从sqlite数据库取含有\n的值 ... -
资料分享 android api dev guid中文(Android开发指南中文版.doc)
2011-03-11 14:12 1406资料分享 android api dev guid中文 ... -
apk反编译问题总结(转)
2011-03-09 15:01 2737http://www.eoeandroid.com/thre ... -
PendingIntent和Intent的区别 转
2011-03-04 14:35 1210Notification notification ... -
转:动态添加/删除spinner菜单--ArrlayList与Widget依赖性
2011-02-24 14:28 1722package irdc.ex04_09; import a ... -
Dialog 2种显示方式
2011-02-23 17:35 1296建议用onCreateDialog(int)和 show ... -
【转】 Array ,Simple ,SimpleCursor Adapter 区别
2011-02-12 16:01 1788原址:http://www.eoeandroid.com ... -
转 【通知 Toast详细用法(显示view)】 【android Toast大全(五种情形)建立属于你自己的Toast】
2011-02-11 10:36 3028原文地址: http://www.pocketdigi. ... -
(转 Android UI学习 - Menu)
2011-02-09 14:19 1347标签:Android menu 原创作品,允许转载,转载时 ...
相关推荐
ShareButton A share button with smooth animation Demo License Copyright 2016 kayan1990 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance ...
Android 超炫的Path Button效果源码.zip项目安卓应用源码下载Android 超炫的Path Button效果源码.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
前几年的Android应用源码Demo,主要面向的是学生毕业设计学习。
Android应用源码开发Demo,主要用于毕业设计学习。
Android Studio 在xml文件中设置界面布局1、xml文件中给输入框editText修改下划线颜色2、设置button按钮为圆角及修改按钮颜色3、运用约束布局时预览效果和运行效果不一致 写一写今天困扰我很久的问题:如何给...
在Android 5.0以后的版本中,定义一个button时,系统自动会加一个阴影的效果,有的时候这种效果看起来比较好,有的时候不符合UI的设计要求,这时候就需要手动去掉阴影。 网上很多文章写了解决办法,就是给button加一...
第3章 Android程序设计基础 3.1 Android程序框架 3.1.1 Android项目目录结构 3.1.2 Android应用解析 3.2 Android的生命周期 3.3 Android程序U设计 3.4 小结 第4章 用户界面开发 4.1 用户界面开发详解 4.1.1 用户...
Android通过画线实现button效果 Android如何防止apk程序被反编译 Android 之 AIDL 和远程 Service 调用 Android 相对布局技巧 android开发环境之Logcat(日志)教程实例汇总 android网络通信之socket教程实例汇总 ...
),效果如下图。 基于老外的这个项目,我将它迁入Android Studio,作了扩展以及打包成aar发布到JCenter中央仓库(可以通过声明依赖的方式引用了,不用再导入项目或拷贝代码到你的项目中)。 对其扩展的目的...
是一个工具包用来帮助设计和开发 Android 用户界面,包含三个单独的工具:Android Asset Studio用户界面原型模具,Android 设计预览,时常需要重复确认程序版面设计状况的 Android App 开发者,应该会爱上这个轻量级...
第3章 Android程序设计基础 3.1 Android程序框架 3.1.1 Android项目目录结构 3.1.2 Android应用解析 3.2 Android的生命周期 3.3 Android程序U设计 3.4 小结 第4章 用户界面开发 4.1 用户界面开发...
不仅详细讲解了Android框架、Android组件、用户界面开发、游戏开发、数据存储、多媒体开发和网络开发等基础知识,而且还深入阐述了传感器、语音识别、桌面组件开发、Android游戏引擎设计、Android应用优化、OpcnGL等...
对于第一部分的设计,主要用到android开发中的布局开发,本设计用到的是线性布局Linearlayout,通过对android布局的使用,从而达到理想的效果,这其中还牵扯到按钮控件Button的使用,总的来说是界面布局与控件结合...
Float Button 圆形进度条效果.zip,圆形进度与fab集成(圆形装载机与浮动动作按钮、物料进度圆和浮动动作按钮集成,fab与进度指示环集成,android,物料设计)。
第3章 Android程序设计基础 3.1 Android程序框架 3.1.1 Android项目目录结构 3.1.2 Android应用解析 3.2 Android的生命周期 3.3 Android程序U设计 3.4 小结 第4章 用户界面开发 4.1 用户界面开发...
Button按钮所触发的事件处理,我们称之为Event Handle,只不过在Android当中,按钮事件是由系统的Button.OnClickListener所控制,熟悉Java程序设计的读者对OnXxxListener应该不陌生.以下的Demo,我们将实现当点击Button时...
只需一个步骤即可使用,也可根据自己项目设计图画布大小和像素尺寸实时精准适配,适配所有市面上各种大小不一的安卓手机屏幕,特点是可以直接在Android studio的XML编辑器中直接预览不同手机屏幕的适配效果,提高...