自定义Button 直接设置selector
来源:互联网 发布:centos 解压 编辑:程序博客网 时间:2024/05/29 21:16
Custom button that can implement click effect don't need create a selector.xml
How To Use
0. create res/values/attrs.xml add code
<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="SelectorButton"><attr name="normal_drawable" format="reference" /><attr name="pressed_drawable" format="reference" /></declare-styleable></resources>
1.Create a class SeletorButton to extends Button
package com.example.mybutton;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.graphics.drawable.StateListDrawable;import android.util.AttributeSet;import android.widget.Button;public class SelectorButton extends Button { public SelectorButton(Context context) { super(context); } public SelectorButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * zyh:normal_drawable="@drawable/btn_login_normal" zyh:pressed_drawable="@drawable/btn_login_pressed" * @param context * @param attrs */ public SelectorButton(Context context, AttributeSet attrs) { super(context, attrs); StateListDrawable seletor = new StateListDrawable();// 背景选择器 Drawable n = null; Drawable p = null; TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SelectorButton); int count = array.getIndexCount();// 获取属性个数 System.out.println("count:" + count); for (int i = 0; i < count; i++) { int index = array.getIndex(i); System.out.println("index:" + index); System.out.println("draw:" + array.getDrawable(index)); switch (index) { case R.styleable.SelectorButton_normal_drawable: n = array.getDrawable(index); break; case R.styleable.SelectorButton_pressed_drawable: p = array.getDrawable(index); break; } } seletor.addState(new int[] { android.R.attr.state_pressed }, p); seletor.addState(new int[] { android.R.attr.state_focused }, p); seletor.addState(new int[] {}, n); this.setBackgroundDrawable(seletor); array.recycle(); }}
2. use in xml
<!-- First you should define a namespace --!> xmlns:zyh="http://schemas.android.com/apk/res/com.example.mybutton"<!--use SelectorButton--!> <com.example.mybutton.SelectorButton android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" zyh:normal_drawable="@drawable/exam_list_bg_default" zyh:pressed_drawable="@drawable/exam_list_bg_pressed" />
example: main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:zyh="http://schemas.android.com/apk/res/com.example.mybutton" 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.mybutton.MainActivity" > <com.example.mybutton.SelectorButton android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" zyh:normal_drawable="@drawable/exam_list_bg_default" zyh:pressed_drawable="@drawable/exam_list_bg_pressed" /></RelativeLayout>
3. use in code.java
SelectorButton mButton = findViewById(R.id.button);Drawable normalDrawable = getResources().getDrawable(R.drawable.btn_normal);Drawable pressedDrawable = getResources().getDrawable(R.drawable.btn_pressed);mButton.setSelecorDrawable(normalDrawable, pressedDrawable);
源码下载:https://github.com/18236887539/SelectorButton
0 0
- 自定义Button 直接设置selector
- button 自定义背景 selector
- 给button设置selector
- Android selector自定义shape的button样式
- 关于给Button设置selector的问题
- AN button自定义样式 && AN selector和shape的使用
- android背景选择器selector用法、自定义Button按钮样式..........
- android背景选择器selector用法、自定义Button按钮样式
- android selector设置button点击效果(详细)以及常见问题
- Android 使用selector设置button字体颜色无效
- iOS button.titleLabel和直接设置button setText
- Android 文字和按钮Button点击效果的(selector使用)直接用
- 给Button指定selector
- Button selector的使用
- Button selector属性
- Android Button之selector
- listview和button -- selector
- android中自定义Button,设置不同背景图片。
- OpenCV训练分类器制作xml文档
- 在服务器操作系统上使用TeamViewer
- 在服务器操作系统上使用TeamViewer
- Windows Server 2008 密码 无法更…
- Windows Server 2008 密码 无法更…
- 自定义Button 直接设置selector
- opencv haartraining 分析一:cvCr…
- opencv haartraining 分析一:cvCr…
- opencv haartraining 分析二:每级…
- opencv haartraining 分析二:每级…
- opencv haartraining 分析三:icvC…
- opencv haartraining 分析三:icvC…
- 运行 CMD竟然转不了目录
- 运行 CMD竟然转不了目录