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

首頁 > 系統 > Android > 正文

Android中搜索圖標和文字居中的EditText實例

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

效果圖:

android,搜索圖標居中,圖標居中

需要自定義view,具體實現如下:

import android/164735.html">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;  }}

在values---attrs下添加

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

搜索圖片

android,搜索圖標居中,圖標居中

在布局文件中調用如下

<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"    />

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 察雅县| 潢川县| 合作市| 邢台市| 和田市| 黄陵县| 襄樊市| 大城县| 双桥区| 佛山市| 鄄城县| 崇义县| 会宁县| 扎兰屯市| 沛县| 团风县| 金华市| 松溪县| 定边县| 哈巴河县| 阳朔县| 繁昌县| 恭城| 迭部县| 梅州市| 望谟县| 庆阳市| 贡觉县| 丰城市| 民和| 兴海县| 镇平县| 绍兴县| 福贡县| 收藏| 龙海市| 多伦县| 连城县| 福鼎市| 石楼县| 比如县|