底部導航Tab圖示 設定可調節圖片大小:
不知怎麼說 ,但是這個很有用;
我實現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]);
}
}
}
}