欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android EditText搜索框实现图标居中

程序员文章站 2022-05-02 21:05:12
类似这样edittext 搜索框,hiht 提示有一个icon并且text内容。 重写edittext : package mobi.truekey...

类似这样edittext 搜索框,hiht 提示有一个icon并且text内容。

Android EditText搜索框实现图标居中

Android EditText搜索框实现图标居中

重写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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。