1.當TextView 設置寬度設置為match_parent的時候
TextView drawablePadding沒有效果 ,字設置了center位置,但是和左邊的圖片離開很遠
2.當TextView 設置的寬度為wrap_parent的時候,extView drawablePadding有效果
解決:自定義TextView
package com.charlie.chpro.customview; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.TextView; import com.charlie.chpro.R; /** * 設置TextView Drawable的大小 * Created by Charlie on 2016/7/30. */ public class ResetDrawableSizeTextView extends TextView { // 需要從xml中讀取的各個方向圖片的寬和高 private int leftHeight = -1; private int leftWidth = -1; private int rightHeight = -1; private int rightWidth = -1; private int topHeight = -1; private int topWidth = -1; private int bottomHeight = -1; private int bottomWidth = -1; public ResetDrawableSizeTextView(Context context) { super(context); } public ResetDrawableSizeTextView(Context context, AttributeSet attrs) { super(context, attrs); // super一定要在我們的代碼之前配置文件 init(context, attrs, 0); } public ResetDrawableSizeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // super一定要在我們的代碼之前配置文件 init(context, attrs, defStyle); } /** * 初始化讀取參數 * */ private void init(Context context, AttributeSet attrs, int defStyle) { // TypeArray中含有我們需要使用的參數 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ResetDrawableSizeTextView, defStyle, 0); if (a != null) { // 獲得參數個數 int count = a.getIndexCount(); int index = 0; // 遍歷參數。先將index從TypedArray中讀出來, // 得到的這個index對應於attrs.xml中設置的參數名稱在R中編譯得到的數 // 這里會得到各個方向的寬和高 for (int i = 0; i < count; i++) { index = a.getIndex(i); switch (index) { case R.styleable.ResetDrawableSizeTextView_bottom_height: bottomHeight = a.getDimensionPixelSize(index, -1); break; case R.styleable.ResetDrawableSizeTextView_bottom_width: bottomWidth = a.getDimensionPixelSize(index, -1); break; case R.styleable.ResetDrawableSizeTextView_left_height: leftHeight = a.getDimensionPixelSize(index, -1); break; case R.styleable.ResetDrawableSizeTextView_left_width: leftWidth = a.getDimensionPixelSize(index, -1); break; case R.styleable.ResetDrawableSizeTextView_right_height: rightHeight = a.getDimensionPixelSize(index, -1); break; case R.styleable.ResetDrawableSizeTextView_right_width: rightWidth = a.getDimensionPixelSize(index, -1); break; case R.styleable.ResetDrawableSizeTextView_top_height: topHeight = a.getDimensionPixelSize(index, -1); break; case R.styleable.ResetDrawableSizeTextView_top_width: topWidth = a.getDimensionPixelSize(index, -1); break; } } // 獲取各個方向的圖片,按照:左-上-右-下 的順序存於數組中 Drawable[] drawables = getCompoundDrawables(); int dir = 0; // 0-left; 1-top; 2-right; 3-bottom; for (Drawable drawable : drawables) { // 設定圖片大小 setImageSize(drawable, dir++); } // 將圖片放回到TextView中 setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]); } } /** * 設定圖片的大小 * */ private void setImageSize(Drawable d, int dir) { if (d == null) { return; } int height = -1; int width = -1; // 根據方向給寬和高賦值 switch (dir) { case 0: // left height = leftHeight; width = leftWidth; break; case 1: // top height = topHeight; width = topWidth; break; case 2: // right height = rightHeight; width = rightWidth; break; case 3: // bottom height = bottomHeight; width = bottomWidth; break; } // 如果有某個方向的寬或者高沒有設定值,則不去設定圖片大小 if (width != -1 && height != -1) { d.setBounds(0, 0, width, height); } } }
xml里面使用
<com.charlie.chpro.customview.ResetDrawableSizeTextView android:id="@+id/ctv_left_title" style="@style/back_toolbar_textview_style" android:layout_gravity="left" android:layout_marginLeft="@dimen/x18" android:drawableLeft="@mipmap/back_normal" android:drawablePadding="@dimen/x6" android:text="@string/back" android:textSize="@dimen/y32" app:left_height="@dimen/y42" app:left_width="@dimen/x24" />