動態設置圖片寬高
TextView textView = new TextView(mContext);
Drawable drawable = getResources().getDrawable(R.drawable.file);
// 設置圖片的大小
drawable.setBounds(0, 0, 80, 80);
// 設置圖片的位置,左、上、右、下
textView.setCompoundDrawables(null, null, drawable, null);
自定義TextView然後在xml中設置圖片寬高
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import com.huaiye.ecs.R;
public class DrawableTextView extends AppCompatTextView {
private Drawable drawableLeft = null, drawableTop = null, drawableRight = null,
drawableBottom = null;
private int drawableWidth, drawableHeight;
public DrawableTextView(Context context) {
this(context, null);
}
public DrawableTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DrawableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
int count = typedArray.getIndexCount();
for(int i = 0; i < count; i++){
int attr = typedArray.getIndex(i);
switch (attr){
case R.styleable.DrawableTextView_drawableRight:
drawableRight = typedArray.getDrawable(attr);
break;
case R.styleable.DrawableTextView_drawableLeft:
drawableLeft = typedArray.getDrawable(attr);
break;
case R.styleable.DrawableTextView_drawableTop:
drawableTop = typedArray.getDrawable(attr);
break;
case R.styleable.DrawableTextView_drawableBottom:
drawableBottom = typedArray.getDrawable(attr);
break;
case R.styleable.DrawableTextView_drawableWidth:
drawableWidth = typedArray.getDimensionPixelSize(attr, 0);
break;
case R.styleable.DrawableTextView_drawableHeight:
drawableHeight = typedArray.getDimensionPixelSize(attr,0);
break;
}
}
if(null != drawableLeft){
drawableLeft.setBounds(0,0, drawableWidth, drawableHeight);
}
if(null != drawableRight){
drawableRight.setBounds(0,0, drawableWidth, drawableHeight);
}
if(null != drawableTop){
drawableTop.setBounds(0,0, drawableWidth, drawableHeight);
}
if(null != drawableBottom){
drawableBottom.setBounds(0,0, drawableWidth, drawableHeight);
}
setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
}
}
TextView新增的屬性
<!-- 圖片文字自定義屬性 -->
<declare-styleable name="DrawableTextView">
<attr name="drawableLeft" format="reference"/>
<attr name="drawableBottom" format="reference"/>
<attr name="drawableRight" format="reference"/>
<attr name="drawableTop" format="reference"/>
<attr name="drawableWidth" format="dimension"/>
<attr name="drawableHeight" format="dimension"/>
</declare-styleable>
使用:新增的屬性得用 app: 作爲開頭
<包名.DrawableTextView
android:id="@+id/tv_upload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="本地上傳"
android:background="@drawable/background_upload"
android:drawablePadding="5dp"
app:drawableTop="@drawable/file"
app:drawableHeight="30dp"
app:drawableWidth="30dp"
/>