返回顶部
首页 > 资讯 > 精选 >Android中搜索图标和文字居中的EditText实例
  • 223
分享到

Android中搜索图标和文字居中的EditText实例

android搜索图标 2023-05-31 01:05:49 223人浏览 独家记忆
摘要

效果图:需要自定义view,具体实现如下:import Android.widget.EditText;import android.content.Context;import android.content.res.TypedArray

效果图:

Android中搜索图标和文字居中的EditText实例

需要自定义view,具体实现如下:

import Android.widget.EditText;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 com.example.administrator.mahu.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.mipmap.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;  }}

--结束END--

本文标题: Android中搜索图标和文字居中的EditText实例

本文链接: https://lsjlt.com/news/222916.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作