这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
本篇文章主要介绍了 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, 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>
搜索图片
在布局文件中调用如下
- <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"
- />
来源: http://www.phperz.com/article/17/0629/336115.html