- 浏览: 84447 次
- 性别:
- 来自: 成都
最新评论
原址:http://marshal.easymorse.com/archives/3059
编写Android自定义按钮
写了个简单的android编写自定义效果的按钮,现在还不完整。不过效果出来了。见:
用手指按压按钮的效果:
手指抬起后,会有Toast提示:
实现按钮,这里没有通过Button类或者子类去做派生,而是通过TextView派生出来的。在这里三个按钮是三个TextView派生类实例,中间的白线,是1px宽的白色矩形,这样就可以做出类似上面的效果。
看布局文件:
<?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" android:background="@drawable/background_color">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="10dip" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="40dip">
<com.easymorse.textbutton.TextButton
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1" android:text="电影"
android:gravity="center_vertical|center_horizontal"
android:background="@drawable/button" android:focusable="true" android:clickable="true"/>
<View android:layout_width="2px" android:layout_height="fill_parent"
android:background="#ffffffff" />
<com.easymorse.textbutton.TextButton
android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true"
android:layout_weight="1" android:text="电视"
android:gravity="center_vertical|center_horizontal"
android:background="@drawable/button" android:focusable="true" />
<View android:layout_width="2px" android:layout_height="fill_parent"
android:background="#ffffffff" />
<com.easymorse.textbutton.TextButton
android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true"
android:layout_weight="1" android:text="明星"
android:gravity="center_vertical|center_horizontal"
android:background="@drawable/button" android:focusable="true" />
</LinearLayout>
</LinearLayout>
这里需要注意的几点:
- 对于布局的像素设置,一般要用dip,这样在更大或者更小的屏幕下展示可以自动适配,如果是px,是物理像素,这样在小的屏幕里面可能会显得大,在大的屏幕中显得小
- 在按钮布局中要使用android:focusable="true" android:clickable="true",这样才能比如通过轨迹球聚焦到按钮上,才能用手触摸按钮的时候触发事件
- 点击按钮变色,主要在android:background="@drawable/button"配置,button配置了点击事件发生后的背景色改变,而不需要编写代码
下面来看看drawable/button.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true">
<item android:state_focused="true">
<shape>
<gradient android:startColor="#FFE5CF33" android:endColor="#FFF1E7A2"
android:angle="90.0">
</gradient>
</shape>
</item>
<item android:state_enabled="true" android:state_pressed="false">
<shape>
<gradient android:startColor="#FF1B1B1B" android:endColor="#FF969696"
android:angle="90.0">
</gradient>
</shape>
</item>
<item android:state_enabled="true" android:state_pressed="true">
<shape>
<gradient android:startColor="#FF000000" android:endColor="#FF474747"
android:angle="90.0">
</gradient>
</shape>
</item>
<item android:state_enabled="false" android:state_pressed="true">
<shape>
<gradient android:startColor="#FF000000" android:endColor="#FF474747"
android:angle="90.0">
</gradient>
</shape>
</item>
<item>
<shape>
<gradient android:startColor="#FF000000" android:endColor="#FF474747"
android:angle="90.0">
</gradient>
</shape>
</item>
</selector>
这个文件中定义了当条目(也就是按钮)enable和(或)pressed的情况下的背景渐近色的配置情况。
实际上,上面介绍的部分,在不使用自定义按钮(也就是不是<com.easymorse.textbutton.TextButton而直接写<TextView…)的情况下,已经可以出现除了点击后Toast消息。
说一下TextView的派生类,其实只是在touche按钮的时候显示提示信息:
package com.easymorse.textbutton;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;public class TextButton extends TextView {
public TextButton(Context context) {
super(context);
}public TextButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}public TextButton(final Context context, AttributeSet attrs) {
this(context, attrs, 0);this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_CANCEL
|| event.getAction() == MotionEvent.ACTION_UP
|| event.getAction() == MotionEvent.ACTION_OUTSIDE) {
Toast.makeText(context, "touched", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}}
在这里主要是写了设置OnTouchListener的代码。
这里只针对3种情况才显示提示:
- 当手指从按钮抬起,ACTION_UP
- 取消,ACTION_CANCEL
- 手指移出按钮,ACTION_OUTSIDE
另外,要返回false,因为返回true,系统将不会调用drawable/button.xml的效果,因为true表示自己已经处理了onTouche事件,不需要别的逻辑再处理了。
源代码见:
发表评论
-
转 android背景选择器selector用法汇总
2011-04-27 15:23 1317原址:http://blog.sina.com.cn/s/bl ... -
(转)android button 效果设计
2011-04-26 15:04 1368原址:http://ssd910.blog.163.com/b ... -
(转)消息机制细谈(message handler looper MessageQueue )
2011-04-12 11:20 1028在handler的简单用 ... -
解决:sqlite数据库含有\n的值或者sax解析xml有\n的值付给TextView,显示却不会换行
2011-03-17 23:00 2905遇到个问题,网上也有人问 从sqlite数据库取含有\n的值 ... -
资料分享 android api dev guid中文(Android开发指南中文版.doc)
2011-03-11 14:12 1405资料分享 android api dev guid中文 ... -
apk反编译问题总结(转)
2011-03-09 15:01 2735http://www.eoeandroid.com/thre ... -
PendingIntent和Intent的区别 转
2011-03-04 14:35 1207Notification notification ... -
转:动态添加/删除spinner菜单--ArrlayList与Widget依赖性
2011-02-24 14:28 1719package irdc.ex04_09; import a ... -
Dialog 2种显示方式
2011-02-23 17:35 1295建议用onCreateDialog(int)和 show ... -
【转】 Array ,Simple ,SimpleCursor Adapter 区别
2011-02-12 16:01 1783原址:http://www.eoeandroid.com ... -
转 【通知 Toast详细用法(显示view)】 【android Toast大全(五种情形)建立属于你自己的Toast】
2011-02-11 10:36 3026原文地址: http://www.pocketdigi. ... -
(转 Android UI学习 - Menu)
2011-02-09 14:19 1344标签:Android menu 原创作品,允许转载,转载时 ...
相关推荐
自定义带进度圈的按钮 --- SubmitButton (kotlin) 使用方法 1、在 gradle 里引用: implementation 'com.chinnsenn.submitbutton:submitbutton:0.1.0' 2、 在 xml 中添加控件 属性也可以在代码中设置 val ...
本资源是自定义带清除按钮的TextView,提供完善的属性设置,包括字长、清除按钮样式、字号、字颜色、hint、text属性设置,并都提供相应的代码设置方法,而且暴露出对应方法获取EditText拉进行更详细比如inputype的...
最近开发中用到许多对话框,之前都是在外面的代码中创建AlertDialog并设置自定义布局实现常见的对话框,诸如更新提示等含有取消和删除两个按钮的对话框我们可以通过代码创建一个AlertDialog并通过它暴露的一系列方法...
在开发中发现uni-app原生导航栏也能实现一些顶部自定义按钮+搜索框,只需在page.json里面做一些配置即可。设置app-plus,配置编译到App平台的特定样式。dcloud平台对app-plus做了详细说明:app-plus配置,需注意 ...
思路:就是先设置Gridlayout的行列数,然后往里面放置一定数目的自定义日历按钮控件,最后实现日历逻辑就可以了。 步骤: 第一步:自定义日历控件(初步) 第二步:实现自定义单个日期按钮控件 第三步:将第二步得到...
6.2.2自定义按钮控件的图片 71 6.3使用按钮控件创建Segmented Control 72 6.4在工具栏创建 Scope Bar 74 第七章 使用日历控件选取日期 (iOS and Android) 76 7.1移动平台中的日历 76 7.2为用户更改的日期编写...
Android自定义View研究-- 一个小Demo Android调用相册拍照实现系统控件缩放切割图片 Android SQLite的实例汇总大全 两分钟彻底让你明白Android Activity生命周期(图文)! Android 图形系统剖析 Android 立体效果图片...
16.2.2 编写Android Google Map骨架程序 409 16.2.3 控制地图 412 16.2.4 地图的显示模式 416 16.2.5 地图的图层 419 16.2.6 查询与定位 422 16.3 Android定位服务 430 16.3.1 开启定位服务 431 16.3.2 模拟...
一个 Android 按钮小部件,可以合理地对齐您的图标和文本。 特征 将图标定位在“文本的左侧”、“文本的右侧”或更标准的“左侧”和“右侧”按钮的极端。 自动调整图标大小以匹配文本大小(文本实际上是此按钮中更...
程序可以采用 JAVA 开发,但是因为它的虚拟机 (Virtual Machine) Dalvik ,是将 JAVA 的 bytecode 转成 自 己的格式,回避掉需要付给 SUN 有关 JAVA 的授权费用。 对手机制造者的影响 � Android 是款开源的移动计算...
任务:制作精美聊天界面 使用自定义的ListView制作精美的聊天界面,编写代码,运行程序,预期效果如下所示。 Android移动应用开发实验指导书全文共13页,当前为第4页。 主界面布局示意图: Android移动应用开发实验...
14.1.2 编写简单的接收程序:示例代码 14.1.3 在描述文件中注册接收程序 14.1.4 发送测试广播 14.1.5 容纳多个接收程序 14.1.6 进程外接收程序项目 14.2 从接收程序使用通知 14.2.1 通过通知管理...
自定义VIEW对话框 (1)运行结果截图 (2)执行代码 代码如下 package com.example.alertdialogdemo; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; ...
您可以通过以下关注我在Twitch上的冒险经历: : 特征可配置的尺寸(行数和列数) 每页最多150个按钮自定义按钮颜色图片(支持:png,jpg和jpeg,webp甚至是GIF!) 形状(圆形,正方形或透明) 和更多.. 轻松保存...
Android提供alert、prompt、pick-list,单选、多选,progress、time-picker和date-picker对话框,并提供自定义的dialog。在Android 3.0后,dialog基于fragment,并对之前版本提供兼容支持库,也就是说对于开发者而言...
通过自定义Holo主题颜色生成对应的Drawable和布局文件 20.dagger-intellij-plugin dagger可视化辅助工具 21.GradleDependenciesHelperPlugin maven gradle 依赖支持自动补全 22.RemoveButterKnife ...
任务:制作精美聊天界面 使用自定义的ListView制作精美的聊天界面,编写代码,运行程序,预期效果如下所示。 Android移动应用开发实验指导书全文共13页,当前为第4页。 主界面布局示意图: Android移动应用开发实验...
在最初寻遍了 GitHub 也没有找到合适的开源库(高度解耦、可远程依赖)之后,我决心研究参考现有开源项目关于二级联动的逻辑,并自己动手编写一个 高度解耦、轻松配置、可通过 maven 仓库远程依赖 的真正的第三方...
16.1 自定义内容提供者的编写步骤 226 16.2 如何使当前应用的内容提供者可以被其他应用访问到 236 17.1 Loader的使用步骤及SearchView的使用 237 17.2 AsyncTaskLoader的使用步骤以及它与CusorLoader的区别 245 18.1...