不知怎么说 ,但是这个很有用;
我实现Fragment 切换的时候tab ,一般是RadioGroup,下的四个RadioButton,然后加四个Fragment实现的;但是这样tab的图标只能由RadioButton的drawableTop指定,不能控制其大小,只能找分辨率小的图片,即使能找到,看上去也不清晰;如果能用分辨率大的图标设置drawableTop的属性并且能指定大小,就完美了。好,这里使用自定义RadioButton的方法;
package com.example.hanlonglin.studyapp.MyView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.RadioButton;
/**
* Created by hanlonglin on 2018/11/15.
*/
public class TabRadioButton extends RadioButton {
Drawable[] drawables;
int width = 50; //这是图标的宽
int height = 50; //这是图标的长
public TabRadioButton(Context context) {
super(context);
init();
}
public TabRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TabRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
overWriteTopDrawable(canvas);
}
private void init() {
drawables = getCompoundDrawables();
}
public void setDrawableSize(int width,int height){
this.width=width;
this.height=height;
invalidate();
}
private void overWriteTopDrawable(Canvas canvas) {
if (drawables != null) {
Drawable drawableTop = drawables[1];
if (drawableTop != null) {
drawableTop.setBounds(0, 0, width, height);
setCompoundDrawables(drawables[0],drawableTop,drawables[2],drawables[3]);
}
}
}
}