国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2019-12-12 02:31:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

重寫(xiě)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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 徐汇区| 洪雅县| 佳木斯市| 宜宾县| 蒙自县| 隆回县| 长子县| 平利县| 青冈县| 开平市| 格尔木市| 措勤县| 洱源县| 开化县| 玉山县| 靖安县| 岳阳县| 柯坪县| 阿拉尔市| 全南县| 东城区| 湖南省| 焦作市| 长寿区| 晴隆县| 博乐市| 绵阳市| 紫阳县| 交城县| 临潭县| 施秉县| 河津市| 祁连县| 平湖市| 巩留县| 西安市| 义马市| 南昌县| 青冈县| 屯昌县| 黄骅市|