引言:格式不熟,有些难看,见谅!
效果图如下:
xml代码:
<!-- 看app属性 注意:默认draw的图片与TextView有一定距离,若要调整使用android:drawablePadding -->
<com.xxxx.DrawableTextView
android:layout_width="wrap_content"
android:layout_height="@dimen/dp20"
android:layout_gravity="center_vertical"//重要必须设置
android:layout_marginRight="@dimen/dp10"
android:background="@drawable/shape_to_add_filter_bg"
android:drawablePadding="@dimen/dp5"
android:gravity="center_vertical"
android:paddingTop="@dimen/dp2"
android:paddingBottom="@dimen/dp2"
android:paddingLeft="@dimen/dp4"
android:paddingRight="@dimen/dp10"
android:text="@string/add_sms_filter"
android:textColor="@color/main_toolbar_text_color"
android:textSize="12sp"
app:left_drawable="@drawable/to_add_filter"//draw的图片
app:drawable_height="12dp"//draw图片高度
app:drawable_width="12dp"//draw图片宽度
/>
思路:自定义TextView
1 自定义属性,
2 重写TextView
3 复制粘贴代码就行了
步骤一:在attr文件内添加
<declare-styleable name="DrawableTextView">
<attr name="left_drawable" format="reference"/>
<attr name="right_drawable" format="reference"/>
<attr name="top_drawable" format="reference"/>
<attr name="bottom_drawable" format="reference"/>
<attr name="drawable_width" format="dimension"/>
<attr name="drawable_height" format="dimension"/>
<attr name="leftdrawable_width" format="dimension"/>
<attr name="lefttdrawable_height" format="dimension"/>
<attr name="rightdrawable_width" format="dimension"/>
<attr name="rightdrawable_height" format="dimension"/>
<attr name="topdrawable_width" format="dimension"/>
<attr name="topdrawable_height" format="dimension"/>
<attr name="bottomdrawable_width" format="dimension"/>
<attr name="bottomdrawable_height" format="dimension"/>
</declare-styleable>
如图:
步骤二:重写TextView (放心复制代码不会报错,只要第一步配置好就行)
代码如下:
public class DrawableTextView extends TextView {
public DrawableTextView(Context context) {
super(context);
}
public DrawableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
int width = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_drawable_width, -1);
int height = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_drawable_height, -1);
SizeWrap sizeWrap = new SizeWrap();
Drawable leftDrawable = ta.getDrawable(R.styleable.DrawableTextView_left_drawable);
if (leftDrawable != null) {
int lwidth = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_leftdrawable_width, -1);
int lheight = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_lefttdrawable_height, -1);
if (sizeWrap.checkWidthAndHeight(width, height, lwidth, lheight)) {
leftDrawable.setBounds(0, 0, sizeWrap.width, sizeWrap.height);
} else {
throw new IllegalArgumentException("error left drawable size setting");
}
}
Drawable rightDrawable = ta.getDrawable(R.styleable.DrawableTextView_right_drawable);
if (rightDrawable != null) {
int rwidth = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_rightdrawable_width, -1);
int rheight = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_rightdrawable_height, -1);
if (sizeWrap.checkWidthAndHeight(width,height, rwidth, rheight)) {
rightDrawable.setBounds(0, 0, sizeWrap.width, sizeWrap.height);
} else {
throw new IllegalArgumentException("error right drawable size setting");
}
}
Drawable topDrawable = ta.getDrawable(R.styleable.DrawableTextView_top_drawable);
if (topDrawable != null) {
int twidth = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_topdrawable_width, -1);
int theight = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_topdrawable_height, -1);
if (sizeWrap.checkWidthAndHeight(width,height, twidth, theight)) {
topDrawable.setBounds(0, 0, sizeWrap.width, sizeWrap.height);
} else {
throw new IllegalArgumentException("error top drawable size setting");
}
}
Drawable bottomDrawable = ta.getDrawable(R.styleable.DrawableTextView_bottom_drawable);
if (bottomDrawable != null) {
int bwidth = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_bottomdrawable_width, -1);
int bheight = ta.getDimensionPixelOffset(R.styleable.DrawableTextView_bottomdrawable_height, -1);
if (sizeWrap.checkWidthAndHeight(width, height, bwidth, bheight)) {
bottomDrawable.setBounds(0, 0, sizeWrap.width, sizeWrap.height);
} else {
throw new IllegalArgumentException("error bottom drawable size setting");
}
}
this.setCompoundDrawables(leftDrawable, topDrawable, rightDrawable, bottomDrawable);
ta.recycle();
ta = null;
}
/**
*
*/
public static class SizeWrap {
int width;
int height;
public boolean checkWidthAndHeight(int globalWidth, int globalHeight, int localWidth, int localHeight) {
width = 0;
height = 0;
//局部的大小设置均正常的情况
if (localWidth > 0 && localHeight > 0) {
width = localWidth;
height = localHeight;
return true;
}
//局部大小没设置时,看全局的大小是否正确设置
if (localWidth == -1 && localHeight == -1) {
if (globalWidth > 0 && globalHeight > 0) {
width = globalWidth;
height = globalHeight;
return true;
}
}
return false;
}
}
@Override
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
if (drawables != null) {
Drawable drawableLeft = drawables[0];
if (drawableLeft != null) {
float textWidth = getPaint().measureText(getText().toString());
int drawablePadding = getCompoundDrawablePadding();
int drawableWidth = 0;
drawableWidth = drawableLeft.getIntrinsicWidth();
float bodyWidth = textWidth + drawableWidth + drawablePadding;
canvas.translate((getWidth() - bodyWidth) / 2, 0);
}
}
super.onDraw(canvas);
}
}
步骤三在xml里引用:
<com.xxx.DrawableTextView
android:layout_width="100dp"
android:layout_height="50dp"
android:text="测试字体"
android:textColor="@color/white"
android:textSize="16sp"
android:gravity="center_vertical"//此处一定是center_vertical,不要写成center
android:drawablePadding="4dp"//用来控制图片和文字之间的距离
app:left_drawable="@drawable/main_icon"//你要设置的图片,可以有right_drawable,top...,bom...
app:drawable_height="12dp"//图片高和宽
app:drawable_width="12dp"
/>
写在最后:按照这个方式可以快速解决你目前的问题。原理日后再研究吧!