在应用程序开发中,经常要对android控件进行自定义来实现界面的风格统一,对网上的资源进行搜索整理如下:
自定义EditText
一, 通过改变背景图片来实现EditText的自定义
(1) 在drawable里添加my_edittext.xml 代码如下:
[xhtml] view plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_focused="true" android:drawable="@drawable/edit1" />
- <item android:drawable="@drawable/edit" />
- </selector>
- 注:正常状态时背景图片为edit,而edit1为EditText为编辑状态
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://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代码如下:
[xhtml] view plaincopyprint?
- <?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>
<?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)在自己的布局文件中加入自定义样式就完成了。
[xhtml] view plaincopyprint?
- <EditText
- android:id="@+id/admin_email"
- android:layout_width="461dip"
- android:layout_height="48dip"
- style="@style/my_edittext_style"
- />
<EditText
android:id="@+id/admin_email"
android:layout_width="461dip"
android:layout_height="48dip"
style="@style/my_edittext_style"
/>
使用上面方式实现的搜索框,布局文件如下:
[xhtml] view plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://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>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://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文件
[c-sharp] view plaincopyprint?
- <?xml version="1.0" encoding="UTF-8"?>
- <selector xmlns:android="http://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>
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://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类
[c-sharp] view plaincopyprint?
- 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);
- }
- }
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)在布局文件中直接引用自定义控件
[xhtml] view plaincopyprint?
- //注意这里 如果不行<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"
- >
//注意这里 如果不行<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文件中,代码如下:
[xhtml] view plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://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>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://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布局文件中的代码:
[xhtml] view plaincopyprint?
- <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"
- />
<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"
/>
分享到: