Android EditText搜索框实现图标居中
更新时间:2017年07月07日 16:34:18 作者:NengLee
本篇文章主要介绍了Android EditText搜索框实现图标居中,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
类似这样EditText 搜索框,hiht 提示有一个icon并且text内容。


重写EditText :
package mobi.truekey.weapp2.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.EditText;
import mobi.truekey.weapp2.R;
public class SearchView extends EditText {
private float searchSize = 0;
private float textSize = 0;
private int textColor = 0xFF000000;
private Drawable mDrawable;
private Paint paint;
public SearchView(Context context, AttributeSet attrs) {
super(context, attrs);
InitResource(context, attrs);
InitPaint();
}
private void InitResource(Context context, AttributeSet attrs) {
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.searchedit);
float density = context.getResources().getDisplayMetrics().density;
searchSize = mTypedArray.getDimension(R.styleable.searchedit_imagewidth, 18 * density + 0.5F);
textColor = mTypedArray.getColor(R.styleable.searchedit_textColor, 0xFF848484);
textSize = mTypedArray.getDimension(R.styleable.searchedit_textSize, 14 * density + 0.5F);
mTypedArray.recycle();
}
private void InitPaint() {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(textColor);
paint.setTextSize(textSize);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
DrawSearchIcon(canvas);
}
private void DrawSearchIcon(Canvas canvas) {
if (this.getText().toString().length() == 0) {
float textWidth = paint.measureText("搜索");
float textHeight = getFontLeading(paint);
float dx = (getWidth() - searchSize - textWidth - 8) / 2;
float dy = (getHeight() - searchSize) / 2;
canvas.save();
canvas.translate(getScrollX() + dx, getScrollY() + dy);
if (mDrawable != null) {
mDrawable.draw(canvas);
}
canvas.drawText("搜索", getScrollX() + searchSize + 8, getScrollY() + (getHeight() - (getHeight() - textHeight) / 2) - paint.getFontMetrics().bottom - dy, paint);
canvas.restore();
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (mDrawable == null) {
try {
mDrawable = getContext().getResources().getDrawable(R.drawable.search);
mDrawable.setBounds(0, 0, (int) searchSize, (int) searchSize);
} catch (Exception e) {
}
}
}
@Override
protected void onDetachedFromWindow() {
if (mDrawable != null) {
mDrawable.setCallback(null);
mDrawable = null;
}
super.onDetachedFromWindow();
}
public float getFontLeading(Paint paint) {
Paint.FontMetrics fm = paint.getFontMetrics();
return fm.bottom - fm.top;
}
}
attr:
<declare-styleable name="searchedit"> <attr name="imagewidth" format="dimension" /> <attr name="textSize" format="dimension" /> <attr name="textColor" format="color" /> </declare-styleable>
drawable背景:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="6dp" /> <solid android:color="@color/white" /> </shape>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android判断用户的网络类型实例讲解(2/3/4G、wifi)
这篇文章主要介绍了Android判断用户的网络类型实例,用户的网络类型分为2G、3G、4G、wifi,通过Android如何判断用户的网络类型,本文为大家揭晓2015-12-12
Android Studio通过Artifactory搭建本地仓库优化编译速度的方法
这篇文章主要介绍了Android Studio通过Artifactory搭建本地仓库优化编译速度的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-03-03
Android textview 实现长按自由选择复制功能的方法
下面小编就为大家带来一篇Android textview 实现长按自由选择复制功能的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-04-04
Android开发 -- 状态栏通知Notification、NotificationManager详解
本文主要讲解状态栏通知Notification、NotificationManager,小编觉得非常详细,给大家一个参考,希望对大家学习有所帮助。2016-06-06
Recyclerview添加头布局和尾布局、item点击事件详解
这篇文章主要为大家详细介绍了Recyclerview添加头布局和尾布局、item点击事件的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-08-08


最新评论