解决的问题:drawableleft,drawableright,drawabletop,drawablebottom与text一起gravity="center"居中的问题。
自定义一个TextView控件,叫做:DrawableCenterTextView.java
package com.dzq.smswclient.widget;
/**
* Data:2016-05-26 16:30
* Created by YJG
*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* drawableTop与文本一起居中显示
* com.yjg.myapplication2.widget.DrawableCenterTextView
*/
public class DrawableCenterTextView extends TextView {
public DrawableCenterTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public DrawableCenterTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawableCenterTextView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
if (drawables != null) {
Drawable drawableTop = drawables[1];
for (int i = 0; i < drawables.length; i++) {
Drawable drawable = drawables[i];
if (drawable != null) {
if (i =