溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android EditText搜索框?qū)崿F(xiàn)圖標(biāo)居中

發(fā)布時間:2020-10-02 11:06:59 來源:腳本之家 閱讀:222 作者:NengLee 欄目:移動開發(fā)

類似這樣EditText 搜索框,hiht 提示有一個icon并且text內(nèi)容。

Android EditText搜索框?qū)崿F(xiàn)圖標(biāo)居中

Android EditText搜索框?qū)崿F(xiàn)圖標(biāo)居中

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI