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)在畫文字的時(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" />
嗯,自己寫這個(gè)東西,也學(xué)到了一些東西,大家有什么更好的方法,大家可以討論一下。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android 側(cè)滑關(guān)閉Activity的實(shí)例
這篇文章主要介紹了Android 側(cè)滑關(guān)閉Activity的實(shí)例的相關(guān)資料,好的手機(jī)現(xiàn)在沒(méi)有物理返回鍵,或者說(shuō)統(tǒng)一Android 與IOS 軟件功能的時(shí)候,需要側(cè)滑關(guān)閉,需要的朋友可以參考下2017-07-07
Android HTTP發(fā)送請(qǐng)求和接收響應(yīng)的實(shí)例代碼
Android HTTP請(qǐng)求和接收響應(yīng)實(shí)例完整的Manifest文件如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-06-06
Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
這篇文章主要介紹了Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了改變ExpandableListView的indicator圖標(biāo)相關(guān)步驟與實(shí)現(xiàn)技巧,涉及Android配置文件的修改,需要的朋友可以參考下2016-03-03
Android實(shí)現(xiàn)進(jìn)度條(ProgressBar)的功能與用法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)進(jìn)度條(ProgressBar)的功能與用法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Android自定義View繪制貝塞爾曲線實(shí)現(xiàn)流程
貝塞爾曲線的本質(zhì)是通過(guò)數(shù)學(xué)計(jì)算的公式來(lái)繪制平滑的曲線,分為一階,二階,三階及多階。但是這里不講數(shù)學(xué)公式和驗(yàn)證,那些偉大的數(shù)學(xué)家已經(jīng)證明過(guò)了,所以就只講講Android開(kāi)發(fā)中的運(yùn)用吧2022-11-11
android分享純圖片到QQ空間實(shí)現(xiàn)方式
今天小編就為大家分享一篇關(guān)于android分享純圖片到QQ空間實(shí)現(xiàn)方式,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04

