Android EditText搜索框?qū)崿F(xiàn)圖標(biāo)居中
更新時(shí)間:2017年07月07日 16:34:18 作者:NengLee
本篇文章主要介紹了Android EditText搜索框?qū)崿F(xiàn)圖標(biāo)居中,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
類似這樣EditText 搜索框,hiht 提示有一個(gè)icon并且text內(nèi)容。


重寫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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android SearchView搜索框組件的使用方法
- Android搜索框SearchView屬性和用法詳解
- Android搜索框組件SearchView的基本使用方法
- Android搜索框通用版
- Android搜索框(SearchView)的功能和用法詳解
- Android開發(fā)之搜索框SearchView用法示例
- Android頂部(toolbar)搜索框?qū)崿F(xiàn)的實(shí)例詳解
- Android利用EditText如何實(shí)現(xiàn)搜索框詳解
- Flutter自定義Appbar搜索框效果
相關(guān)文章
android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片
這篇文章主要為大家詳細(xì)介紹了android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
將cantk runtime嵌入到現(xiàn)有的APP中的方法
今天小編就為大家分享一篇關(guān)于將cantk runtime嵌入到現(xiàn)有的APP中的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Android中fragment+viewpager實(shí)現(xiàn)布局
這篇文章主要為大家詳細(xì)介紹了Android中fragment+viewpager實(shí)現(xiàn)布局效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

