Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android中搜索图标和文字居中的EditText实例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

效果图:

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,(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;
  }

}

在values---attrs下添加

<declare-styleable name="searchedit">
    <attr name="imagewidth" format="dimension" />
    <attr name="textSize" format="dimension" />
    <attr name="textColor" format="color" />
 </declare-styleable>

搜索图片

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


在布局文件调用如下

<com.example.administrator.mahu.view.SearchView
    android:id="@+id/search"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layouT_Below="@+id/layout"
    android:BACkground="@drawable/search_kuang"
    android:textSize="17sp"
    android:paddingLeft="5dp"
    android:singleLine="true"
    android:imeOptions="actionSearch"
    />

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

大佬总结

以上是大佬教程为你收集整理的Android中搜索图标和文字居中的EditText实例全部内容,希望文章能够帮你解决Android中搜索图标和文字居中的EditText实例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。