Android自定义控件及按下效果
在应用程序开发中,经常要对android控件进行自定义来实现界面的风格统一,对网上的资源进行搜索整理如下:
自定义EditText
一, 通过改变背景图片来实现EditText的自定义
(1) 在drawable里添加my_edittext.xml 代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="https://schemas.android.com/apk/res/android">
- <item android:state_focused="true" android:drawable="@drawable/edit1" />
- <item android:drawable="@drawable/edit" />
- </selector>
- 注:正常状态时背景图片为edit,而edit1为EditText为编辑状态
(2) 在value文件下添加 my_edittext_style.xml代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="suretouch_edittext_style" parent="@android:style/Widget.EditText">
- //引入drawable下的my_edittext.xml文件
- <item name="android:background">@drawable/my_edittext</item>
- </style>
- </resources>
(3)在自己的布局文件中加入自定义样式就完成了。
- <EditText
- android:id="@+id/admin_email"
- android:layout_width="461dip"
- android:layout_height="48dip"
- style="@style/my_edittext_style"
- />
使用上面方式实现的搜索框,布局文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="260dip">
- <EditText
- android:id="@+id/findEditText"
- android:layout_alignParentTop="true"
- android:singleLine="true"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="请输入查询的类容"/>
- <ImageView
- android:id="@+id/findButton"
- android:src="@drawable/findbutton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="4dip"
- android:layout_marginRight="2dip"
- android:adjustViewBounds="true"
- android:layout_alignTop="@id/findEditText"
- android:layout_alignRight="@id/findEditText"
- android:layout_alignBottom="@id/findEditText"/>
- </RelativeLayout>
drawable中的xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <selector xmlns:android="https://schemas.android.com/apk/res/android">
- <item android:state_pressed="true" android:drawable="@drawable/find0" />
- <item android:state_focused="true" android:drawable="@drawable/find1" />
- <item android:state_enabled="true" android:drawable="@drawable/find1" />
- </selector>
图片
二,自定义EditText的边框
(1)创建自定义MyEditText类
- package com.util;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.EditText;
- public class MyEditText extends EditText {
- private Paint mPaint;
- private Context mContext;
- public MyEditText(Context context) {
- super(context);
- }
- // public MyEditText(Context context, AttributeSet attrs, int defStyle) {
- // super(context, attrs, defStyle);
- // // TODO Auto-generated constructor stub
- // }
- //
- public MyEditText(Context context, AttributeSet attrs) {
- super(context, attrs);
- mContext=context;
- // TODO Auto-generated constructor stub
- //定义画笔
- mPaint = getPaint();
- //定义笔画粗细样式
- mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
- //定义笔画颜色
- mPaint.setColor(Color.GRAY);
- }
- public void onDraw(Canvas canvas){
- super.onDraw(canvas);
- int w = getWidth();
- int h = getHeight();
- //下划线
- // canvas.drawLine(0,h-1, w-1, h-1, mPaint);
- //下边框
- canvas.drawLine(0, h, w, h, mPaint);
- //右边框
- canvas.drawLine(w, 0, w, h, mPaint);
- //左边框
- canvas.drawLine(0, 0, 0, h, mPaint);
- //上边框
- canvas.drawLine(0, 0, w, 0, mPaint);
- }
- }
(2)在布局文件中直接引用自定义控件
- //注意这里 如果不行<View class="com.util.MyEditText">
- <com.util.MyEditText
- android:id="@+id/register_password"
- android:layout_width="461dip"
- android:layout_height="48dip"
- android:layout_x="398dip"
- android:layout_y="353dip"
- android:password="true"
- style="?android:attr/textViewStyle"
- android:background="@null"
- android:textColor="@null"
- >
图
三,按钮按下效果,非常简单同样是使用selector
(1) 在drawable中的xml文件中,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="https://schemas.android.com/apk/res/android">
- <item android:state_pressed="false"
- android:drawable="@drawable/register_up" />
- <item android:state_pressed="true"
- android:drawable="@drawable/register_down" />
- <item android:state_focused="true"
- android:drawable="@drawable/register_down" />
- <item android:drawable="@drawable/register_up" />
- </selector>
(2) ImageButton在xml布局文件中的代码:
- <ImageButton
- android:id="@+id/register_btn"
- android:src="@drawable/suretouch_register_button"
- android:layout_width="158dip"
- android:layout_height="149dip"
- android:layout_x="893dip"
- android:layout_y="270dip"
- android:scaleType="centerCrop"
- android:background="@null"
- />
最后更新:2017-04-02 16:48:06
上一篇:
android:MotionEvent
下一篇:
Android自定义button的实现,未选中,按下,选中效果
ios ipa apple company 开发者账号申请分享攻略
多条记录求和问题
Speeding up Migration on ApsaraDB for Redis
3.3.4 PCI设备进行DMA写时发生Cache命中
数据工程师的崛起
J2EE中HTTP method GET/Post is not supported by this URL
相关聊天工具 微(陆(台球热赛娱乐赛每晚南口路底(水树奈(闫(中华人民共和国税收征收管理法(主席令第四十九号) 2015年8月15日 - 会关于修改〈中华人民共和国文物保护法〉等十二部法律的决定》(主席令第...第八十九条 纳税人、扣缴义务人可以委托税务代
用shell脚本合并多个文件内容
java.util.concurrent包(4)——读写锁ReentrantReadWriteLock
SQL COUNT(column_name) 语法