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

首頁 > 系統 > Android > 正文

Android EditText搜索框實現圖標居中

2019-10-22 18:32:48
字體:
來源:轉載
供稿:網友

類似這樣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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乃东县| 辽源市| 江津市| 武威市| 闽侯县| 洛阳市| 松原市| 五原县| 星座| 金坛市| 个旧市| 乌兰浩特市| 廉江市| 山阳县| 新营市| 遂川县| 镇康县| 安平县| 满洲里市| 高唐县| 白城市| 庆云县| 宜宾市| 汾阳市| 思茅市| 永仁县| 木里| 突泉县| 农安县| 莱西市| 龙门县| 泰兴市| 普陀区| 盐城市| 固安县| 德保县| 盈江县| 金川县| 兴和县| 富蕴县| 峨边|