Android 自定义Button控件实现按钮点击变色
时间:2022-12-04 18:18:46
效果图如下所示:
一、shape 样式:(在drawable新建--》new--》Drawable resource file 在父级标签selector添加Item )
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#73c4f3" />
<stroke android:width="2dp" android:color="#ffffff" />
<corners android:radius="20dp" />
<padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" />
<gradient android:angle="270" android:endColor="#8accf2" android:startColor="#8accf2" android:type="sweep" />
</shape>
<shape>
<solid android:color="#3fb3f6" />
<stroke android:width="2dp" android:color="#ffffff" />
<corners android:radius="20dp" />
<padding android:bottom="5dp" android:left="10dp" android:right="10dp" android:top="5dp" />
</shape>
</item>
</selector>
二、style 样式:
<style name="rectangle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">15sp</item>
<item name="android:textColor">#ffffff</item>
<item name="android:background">@drawable/buttonclickstyle</item>
</style>
三、Button控件调用style样式:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ly.blogtest.MainActivity">
<Button
android:id="@+id/button"
style="@style/rectangle"
android:text="@string/btn1" />
</RelativeLayout>
----------------------------------按钮点击变色-----------------------------------------------------
----------------------------------item属性标记------------------------------
android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一个RadioButton可以被check了。
android:state_enabled 能够接受触摸或者点击事件
android:state_activated 被激活
android:state_window_focused
应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了
注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)
如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。
----------------------------------item属性标记------------------------------
以上所述是小编给大家介绍的Android 自定义Button控件实现按钮点击变色网站的支持!
来源:http://www.cnblogs.com/LOVEJIEYING/archive/2016/11/03/6025563.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
BufferedInputStream(缓冲输入流)详解_动力节点Java学院整理
JDK1.8中的ConcurrentHashMap源码分析
![](https://img.aspxhome.com/file/2023/1/60481_0s.jpg)
WebService教程详解(二)
idea生成类注释和方法注释的正确方法(推荐)
![](https://img.aspxhome.com/file/2023/4/85554_0s.jpg)
详解Java异常处理中finally子句的运用
Spring mvc拦截器实现原理解析
解析Java的设计模式编程之解释器模式的运用
![](https://img.aspxhome.com/file/2023/5/63755_0s.jpg)
SpringBoot整合SSO(single sign on)单点登录
![](https://img.aspxhome.com/file/2023/7/66517_0s.jpg)
C#获取web.config配置文件内容的方法
c#调用c++的DLL的实现方法
![](https://img.aspxhome.com/file/2023/9/71769_0s.png)
Android 自定义View实现芝麻分曲线图效果
![](https://img.aspxhome.com/file/2023/3/138873_0s.png)
Java内存模型之happens-before概念详解
![](https://img.aspxhome.com/file/2023/1/59971_0s.png)
SpringBoot实现RAS+AES自动接口解密
![](https://img.aspxhome.com/file/2023/5/119785_0s.jpg)
C#实现Check Password和锁定输错密码锁定账户功能
基于FLink实现实时安全检测的示例代码
Android权限HaloPermission详细使用
unity 切换场景不销毁物体问题的解决
Java线程的生命周期命名与获取代码实现
![](https://img.aspxhome.com/file/2023/5/64145_0s.jpg)
Android在项目中接入腾讯TBS浏览器WebView的教程与注意的地方
![](https://img.aspxhome.com/file/2023/5/139505_0s.png)