Android中搜索圖標(biāo)和文字居中的EditText實(shí)例
效果圖:
需要自定義view,具體實(shí)現(xiàn)如下:
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>
搜索圖片
在布局文件中調(diào)用如下
<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" />
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android @override 報(bào)錯(cuò)解決方案
android @override 報(bào)錯(cuò):就是說(shuō)Java 1.5的編譯器默認(rèn)對(duì)父類的方法進(jìn)行覆蓋,采用@Override進(jìn)行說(shuō)明;但1.6已經(jīng)擴(kuò)展到對(duì)接口的方法;所以如果還是以Java 1.5的編譯器來(lái)編譯的話,會(huì)出現(xiàn)錯(cuò)誤2012-12-12Android實(shí)現(xiàn)連續(xù)點(diǎn)擊多次事件的代碼詳解
小編經(jīng)常遇到這樣的需求類似進(jìn)入開(kāi)發(fā)者模式,即多次點(diǎn)擊后執(zhí)行操作。下面小編通過(guò)實(shí)例代碼給大家講解Android實(shí)現(xiàn)連續(xù)點(diǎn)擊多次事件的相關(guān)知識(shí),感興趣的朋友跟隨小編一起學(xué)習(xí)吧2018-10-10Android利用Badge組件實(shí)現(xiàn)未讀消息小紅點(diǎn)
在?App?的運(yùn)營(yíng)中,活躍度是一個(gè)重要的指標(biāo),日活/月活……為了提高活躍度,就發(fā)明了小紅點(diǎn)。這一篇,來(lái)介紹一個(gè)徽標(biāo)(Badge)組件,能夠快速搞定應(yīng)用內(nèi)的小紅點(diǎn),希望對(duì)大家有所幫助2023-01-01Android 加載assets中的資源文件實(shí)例代碼
這篇文章主要介紹了Android 加載assets中的資源文件實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,需要的朋友可以參考下2017-01-01Android 自定義imageview實(shí)現(xiàn)圖片縮放實(shí)例詳解
這篇文章主要介紹了Android 自定義imageview實(shí)現(xiàn)圖片縮放實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04Android 8.0實(shí)現(xiàn)發(fā)送通知
這篇文章主要為大家詳細(xì)介紹了Android 8.0實(shí)現(xiàn)發(fā)送通知,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android 啟動(dòng)第三方程序的方法總結(jié)
這篇文章主要介紹了Android 啟動(dòng)第三方程序的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04RxJava和Retrofit2的統(tǒng)一處理單個(gè)請(qǐng)求示例詳解
這篇文章主要給大家介紹了關(guān)于RxJava和Retrofit2的統(tǒng)一處理單個(gè)請(qǐng)求的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11