底部導航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]);

}

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章