Android 自定义Button控件实现按钮点击变色

效果图如下所示:

一、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控件实现按钮点击变色,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

时间: 2024-09-09 02:36:56

Android 自定义Button控件实现按钮点击变色的相关文章

Android 自定义Button控件实现按钮点击变色_Android

效果图如下所示: 一.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 and

Android自定义组合控件之自定义下拉刷新和左滑删除实例代码_Android

绪论 最近项目里面用到了下拉刷新和左滑删除,网上找了找并没有可以用的,有比较好的左滑删除,但是并没有和下拉刷新上拉加载结合到一起,要不就是一些比较水的结合,并不能在项目里面使用,小编一着急自己组合了一个,做完了和QQ的对比了一下,并没有太大区别,今天分享给大家,其实并不难,但是不知道为什么网上没有比较好的Demo,当你的项目真的很急的时候,又没有比较好的Demo,那么"那条友谊的小船儿真是说翻就翻啊",好了,下面先来具体看一下实现后的效果吧:   代码已经上传到Github上了,小伙伴

Android自定义View控件实现刷新效果_Android

三种得到LinearInflater的方法 a. LayoutInflater inflater = getLayoutInflater(); b. LayoutInflater localinflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); c. LayoutInflater inflater = LayoutInflater.from(context); onDraw 方法

Android自定义View控件实现刷新效果

三种得到LinearInflater的方法 a. LayoutInflater inflater = getLayoutInflater(); b. LayoutInflater localinflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); c. LayoutInflater inflater = LayoutInflater.from(context); onDraw 方法

Android自定义表格控件满足人们对视觉的需求_Android

Android平台已经给我们提供了很多标准的组件,如:TextView.EditView.Button.ImageView.Menu等,还有许多布局控件,常见的有:AbsoluteLayout.LinerLayout.RelativeLayout.TableLayout等.但随着人们对视觉的需求,基本组件已无法满足人们求新求异的要求,于是我们常常会自定义组件,用来实现更美观的UI界面. 实现自定义控件通常有两种途径,一种是继承View类,重写其中的重要方法,另一种是继承ViewGroup类,通过

Android自定义日历控件实例详解_Android

为什么要自定义控件 有时,原生控件不能满足我们对于外观和功能的需求,这时候可以自定义控件来定制外观或功能:有时,原生控件可以通过复杂的编码实现想要的功能,这时候可以自定义控件来提高代码的可复用性. 如何自定义控件 下面我通过我在github上开源的Android-CalendarView项目为例,来介绍一下自定义控件的方法.该项目中自定义的控件类名是CalendarView.这个自定义控件覆盖了一些自定义控件时常需要重写的一些方法. 构造函数 为了支持本控件既能使用xml布局文件声明,也可在ja

android自定义倒计时控件示例_Android

自定义TextView控件TimeTextView代码: 复制代码 代码如下: import android.content.Context;import android.content.res.TypedArray;import android.graphics.Paint;import android.text.Html;import android.util.AttributeSet;import android.widget.TextView; import com.new0315.R;

android自定义倒计时控件示例

自定义TextView控件TimeTextView代码: 复制代码 代码如下:import android.content.Context;import android.content.res.TypedArray;import android.graphics.Paint;import android.text.Html;import android.util.AttributeSet;import android.widget.TextView; import com.new0315.R;/

Android自定义日历控件实例详解

为什么要自定义控件 有时,原生控件不能满足我们对于外观和功能的需求,这时候可以自定义控件来定制外观或功能:有时,原生控件可以通过复杂的编码实现想要的功能,这时候可以自定义控件来提高代码的可复用性. 如何自定义控件 下面我通过我在github上开源的Android-CalendarView项目为例,来介绍一下自定义控件的方法.该项目中自定义的控件类名是CalendarView.这个自定义控件覆盖了一些自定义控件时常需要重写的一些方法. 构造函数 为了支持本控件既能使用xml布局文件声明,也可在ja