Android編程中TextView寬度過(guò)大導(dǎo)致Drawable無(wú)法居中問(wèn)題解決方法
本文實(shí)例講述了Android編程中TextView寬度過(guò)大導(dǎo)致Drawable無(wú)法居中問(wèn)題解決方法。分享給大家供大家參考,具體如下:
在做項(xiàng)目的時(shí)候,很多時(shí)候我們都要用到文字和圖片一起顯示,一般設(shè)置TextView的DrawableLeft、DrawableRight、DrawableTop、DrawableBottom就行了。但是有一種情況是當(dāng)TextView的熟悉是fill_parent或者使用權(quán)重的時(shí)候并且設(shè)置了起Gravity的ceter的時(shí)候,Drawable圖片是無(wú)法一起居中的,為了解決其,我們一般再套一層布局,然后設(shè)置TextView的熟悉是wrap_content,但是有時(shí)候嵌套過(guò)多的布局的時(shí)候,有可能發(fā)生StackOverFlow,所以必須要優(yōu)化,下面說(shuō)一下其中的一個(gè)解決方案。先上圖
這個(gè)解決方案很粗糙,局限性很大,文字不能換行,換行之后就不準(zhǔn)了,下面是源碼:
package com.example.testandroid; import java.lang.ref.WeakReference; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.TextView; public class DrawableTextView extends TextView { private WeakReference<Bitmap> normalReference; private WeakReference<Bitmap> pressReference; private WeakReference<Bitmap> showReference; private int normalColor = Color.WHITE, pressColor = Color.WHITE; private String text; private int textWidth = 0; private int textHeight = 0; public DrawableTextView(Context context) { super(context); } public DrawableTextView(Context context, AttributeSet attrs) { super(context, attrs); } public DrawableTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onFinishInflate() { super.onFinishInflate(); initText(); } private void initText() { text = super.getText().toString(); initVariable(); } /** * 初始化,測(cè)量Textview內(nèi)容的長(zhǎng)度,高度 */ private void initVariable() { textWidth = (int) (getPaint().measureText(text)); final Rect rect = new Rect(); getPaint().getTextBounds(text, 0, 1, rect); textHeight = rect.height(); } /** * 設(shè)置TextView的內(nèi)容 * @param text */ public void setText(String text) { this.text = text; initVariable(); invalidate(); } /** * 獲取TextView內(nèi)容 */ public String getText() { return text; } /** * 設(shè)置TextView的Drawable內(nèi)容,目前僅支持DrawableLeft * @param normalDrawableId * DrawableLeft的normal狀態(tài)Id * @param pressDrawableId * DrawableLeft的press狀態(tài)的Id(沒(méi)有press狀態(tài),請(qǐng)傳-1) */ public void setDrawableLeftId(final int normalDrawableId, final int pressDrawableId) { normalReference = new WeakReference<Bitmap>(BitmapFactory.decodeResource(getResources(), normalDrawableId)); if (pressDrawableId != -1) { pressReference = new WeakReference<Bitmap>(BitmapFactory.decodeResource(getResources(), pressDrawableId)); } showReference = normalReference; invalidate(); } /** * 設(shè)置TextView的Color * @param normalColor * TextView normal狀態(tài)的Color值 * @param pressDrawableId * TextView press狀態(tài)的Color值(如果沒(méi)有press狀態(tài),請(qǐng)傳與normal狀態(tài)的值) */ public void setTextColor(final int normalColor, final int pressColor) { this.normalColor = normalColor; this.pressColor = pressColor; getPaint().setColor(normalColor); initVariable(); } @Override protected void onDraw(Canvas canvas) { if (showReference != null && showReference.get() != null) { final int bitmapWidth = showReference.get().getWidth(); final int bitmapHeight = showReference.get().getHeight(); final int viewHeight = getHeight(); final int drawablePadding = getCompoundDrawablePadding(); final int start = (getWidth() - (bitmapWidth + drawablePadding + textWidth)) >> 1; canvas.drawBitmap(showReference.get(), start, (viewHeight >> 1) - (bitmapHeight >> 1), getPaint()); /** * 注意改方法,第三個(gè)參數(shù)y,本人也被誤導(dǎo)了好久,原來(lái)在畫(huà)文字的時(shí)候,y表示文字最后的位置(不是下筆點(diǎn)的起始位置) * 所以為什么 是TextView高度的一半(中間位置) + 文字高度的一半 = 文字居中 */ canvas.drawText(text, start + drawablePadding + bitmapWidth, (viewHeight >> 1) + (textHeight >> 1), getPaint()); } } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (pressReference != null && pressReference.get() != null) { showReference = pressReference; } getPaint().setColor(pressColor); } else if (event.getAction() == MotionEvent.ACTION_UP) { if (normalReference != null && normalReference.get() != null) { showReference = normalReference; } getPaint().setColor(normalColor); } invalidate(); return super.onTouchEvent(event); } }
xml布局:
<com.example.testandroid.DrawableTextView android:id="@+id/my_textview" android:layout_width="fill_parent" android:layout_marginTop="20dp" android:background="@drawable/text_selector" android:drawablePadding="8dp" android:textColor="@color/standard_orange" android:layout_height="wrap_content" android:padding="15dp" android:textSize="16sp" android:text="有Drawable的TextView" />
調(diào)用代碼:
DrawableTextView drawableTextView = (DrawableTextView) getView().findViewById(R.id.my_textview); drawableTextView.setDrawableLeftId(R.drawable.bg_btn_delete_normal, R.drawable.bg_btn_delete_pressed); drawableTextView.setTextColor(getResources().getColor(R.color.standard_orange), getResources().getColor(R.color.standard_white)); drawableTextView.setText("我在動(dòng)態(tài)修改Text啦");
其實(shí)還有更加方便的方法,下面朋友借鑒某個(gè)網(wǎng)友的代碼(地址我就不知道了):
@Override protected void onDraw(Canvas canvas) { Drawable[] drawables = getCompoundDrawables(); if (drawables != null) { Drawable drawableLeft = drawables[0]; if (drawableLeft != null) { final float textWidth = getPaint().measureText(getText().toString()); final int drawablePadding = getCompoundDrawablePadding(); final int drawableWidth = drawableLeft.getIntrinsicWidth(); final float bodyWidth = textWidth + drawableWidth + drawablePadding; canvas.translate((getWidth() - bodyWidth) / 2, 0); } } super.onDraw(canvas); }
xml布局:
<com.example.testandroid.DrawableTextView android:id="@+id/my_textview" android:layout_width="fill_parent" android:layout_marginTop="20dp" android:background="@drawable/text_selector" android:drawablePadding="8dp" android:drawableLeft="@drawable/clear_edittext_selector" android:textColor="@color/text_color_selector" android:layout_height="wrap_content" android:padding="15dp" android:textSize="16sp" android:text="有Drawable的TextView" />
嗯,自己寫(xiě)這個(gè)東西,也學(xué)到了一些東西,大家有什么更好的方法,大家可以討論一下。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android View實(shí)現(xiàn)圓形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android View實(shí)現(xiàn)圓形進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Android捕捉錯(cuò)誤try catch 的簡(jiǎn)單使用教程
這篇文章主要介紹了Android捕捉錯(cuò)誤try catch 的簡(jiǎn)單使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Android自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07Android實(shí)現(xiàn)recyclerview城市字母索引列表
大家好,本篇文章主要講的是Android實(shí)現(xiàn)recyclerview城市字母索引列表,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
本篇文章小編為大家介紹,Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法。需要的朋友參考下2013-04-04Android 判斷程序在前臺(tái)運(yùn)行還是后臺(tái)運(yùn)行
本文主要介紹了Android 判斷程序在前臺(tái)運(yùn)行還是后臺(tái)運(yùn)行的方法。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04Android自定義控件(實(shí)現(xiàn)視圖樹(shù)繪制指示器)
本文主要介紹了Android視圖樹(shù)繪制指示器的實(shí)現(xiàn)原理和具體步驟。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01Android?Flutter實(shí)現(xiàn)自由落體彈跳動(dòng)畫(huà)效果
粒子運(yùn)動(dòng)是將對(duì)象按照一定物理公式進(jìn)行的自定義軌跡運(yùn)動(dòng),與普通動(dòng)畫(huà)不同的是,它沒(méi)有強(qiáng)制性的動(dòng)畫(huà)開(kāi)始到結(jié)束的時(shí)間概念。本文將利用Flutter實(shí)現(xiàn)自由落體彈跳動(dòng)畫(huà)效果,感興趣的小伙伴可以學(xué)習(xí)一下2022-10-10Android中庫(kù)項(xiàng)目的使用方法圖文介紹
類似開(kāi)發(fā)其他Java應(yīng)用一樣,我們可以將可復(fù)用的代碼,打成一個(gè)jar包,供所有需要的項(xiàng)目使用。這樣,可以解決很大一部分代碼復(fù)用的問(wèn)題,本文將詳細(xì)介紹,需要了解的朋友可以參考下2012-12-12