android edittext 文字偏上,Android中搜索图标和文字居中的EditText实例

效果图:

8e5ea4d1bd221d5776e5ded6916c85ce.png

需要自定义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,(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下添加

de5e72de097eb453296e4434a45fd435.png

在布局文件中调用如下

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"

/>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

小编个人微信号 jb51ccc

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值