android edittext hint文字居中,自定义EditText设置图片文字居中

这篇博客介绍了如何在Android中创建一个自定义的EditText,使得hint文字和左侧的图片都能居中显示。通过继承AppCompatEditText并重写部分方法,实现了在获取和失去焦点时改变文字对齐方式,并在布局时计算了图片与文字的间距,确保整体居中效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA

自定义类继承EditText or AppCompatEditText

import android.content.Context;

import android.graphics.Paint;

import android.graphics.Rect;

import android.graphics.drawable.Drawable;

import android.support.v7.widget.AppCompatEditText;

import android.util.AttributeSet;

import android.view.View;

import android.widget.TextView;

public class SearchText extends AppCompatEditText {

private Drawable searchDrawable;

private int offset;

private int searchWidth;

private String hintString;

private int w;

private int flag = 0;

public SearchText(Context context) {

super(context);

init();

}

public SearchText(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public SearchText(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

private void init() {

setOnFocusChangeListener(new OnFocusChangeListener() {

@Override

public void onFocusChange(View v, boolean hasFocus) {

if (hasFocus) {

// 获得焦点

setTextAlignment(TextView.TEXT_ALIGNMENT_VIEW_START);

} else {

// 失去焦点

setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);

}

}

});

setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);

}

@Override

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

super.onLayout(changed, left, top, right, bottom);

//圈1

searchWidth = getMeasuredWidth();

hintString = getHint().toString();

//圈2

Paint paint = new Paint();

Rect rect = new Rect();

paint.getTextBounds(hintString, 0, hintString.length(), rect);

w = dip2px(getContext(), rect.width());

offset = searchWidth / 2 - w * 2;

if (flag == 0) {

//圈3 //圈4

setTextDrawable();

}

flag++;

}

public static int dip2px(Context context, float dpValue) {

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (dpValue * scale + 0.5f);

}

@Override

protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {

super.onTextChanged(text, start, lengthBefore, lengthAfter);

if (searchDrawable == null) {

getDrawable();

}

if (length() > 0) {

setTextAlignment(TextView.TEXT_ALIGNMENT_VIEW_START);

setCompoundDrawables(null, null, null, null);

} else if (length() == 0) {

setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);

setTextDrawable();

}

}

void getDrawable() {

//圈5

Drawable[] compoundDrawables = getCompoundDrawables();

searchDrawable = compoundDrawables[0];

}

void setTextDrawable() {

searchDrawable.setBounds(offset, 0, offset + searchDrawable.getIntrinsicWidth(), searchDrawable.getIntrinsicHeight());

setCompoundDrawables(searchDrawable, null, null, null);

}

}

XML(包名改成自己的)

android:id="@+id/work_detail_deal_research"

android:layout_width="match_parent"

android:layout_height="30dp"

android:background="@color/white"

android:layout_marginBottom="2dp"

android:layout_gravity="center"

android:gravity="center"

android:drawableLeft="@drawable/research"

android:hint="搜索"

android:textSize="@dimen/txtSize_title"

android:textColor="@color/txt_color1"/>

图标

180ae2fb8f71b1cb7ee068d6f9b4b619.png

80fe919def51544a65e56df603ba5136.png

10967331b956eea84068e05ef9198a91.png

dimon

16dp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值