android按钮控件留白,Android实现FloatingActionButton自定义大小 · JackRo 的个人博客

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

根据官方文档的说明,FloatingActionButton 仅仅支持设置app:fabSize=”normal”来改变fab的大小,并且这个fabSize的大小提供了三个可选项(normal,mini和auto)来设置,可以在布局里设置也可以在代码里调用setSize方法进行设置。

这里一开始是想到通过在布局里改变layout_width和layout_height来改变控件的大小的,但是设置之后(两者均设置为100dp,fabSize设置为normal),效果如下所示:

508dbbc176aaabae593c9f9fcd2874f3.png

并且当设置了fab的固定宽高再来改变它的fabSize属性(改为mini或auto)时,效果会是fab的图标被撑的很大,甚至边缘都看不到了,显然这样通过固定fab的宽高属性来改变其大小是行不通的。

翻阅FloatingActionButton的源码,其第三个构造函数如下public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

ThemeUtils.checkAppCompatTheme(context);

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.FloatingActionButton, defStyleAttr,

R.style.Widget_Design_FloatingActionButton);

mBackgroundTint = a.getColorStateList(R.styleable.FloatingActionButton_backgroundTint);

mBackgroundTintMode = ViewUtils.parseTintMode(a.getInt(

R.styleable.FloatingActionButton_backgroundTintMode, -1), null);

mRippleColor = a.getColor(R.styleable.FloatingActionButton_rippleColor, 0);

mSize = a.getInt(R.styleable.FloatingActionButton_fabSize, SIZE_AUTO);

mBorderWidth = a.getDimensionPixelSize(R.styleable.FloatingActionButton_borderWidth, 0);

final float elevation = a.getDimension(R.styleable.FloatingActionButton_elevation, 0f);

final float pressedTranslationZ = a.getDimension(

R.styleable.FloatingActionButton_pressedTranslationZ, 0f);

mCompatPadding = a.getBoolean(R.styleable.FloatingActionButton_useCompatPadding, false);

a.recycle();

mImageHelper = new AppCompatImageHelper(this);

mImageHelper.loadFromAttributes(attrs, defStyleAttr);

mMaxImageSize = (int) getResources().getDimension(R.dimen.design_fab_image_size);

getImpl().setBackgroundDrawable(mBackgroundTint, mBackgroundTintMode,

mRippleColor, mBorderWidth);

getImpl().setElevation(elevation);

getImpl().setPressedTranslationZ(pressedTranslationZ);

}

都是读取属性来设置一些自定义属性,看到最后三行,发现getImpl方法的调用,它的返回值是FloatingActionButtonImpl,原来fab都是通过FloatingActionButtonImpl来实现其自定义属性的,找到getImpl这个方法,源码如下:private FloatingActionButtonImpl getImpl() {

if (mImpl == null) {

mImpl = createImpl();

}

return mImpl;

}

private FloatingActionButtonImpl createImpl() {

final int sdk = Build.VERSION.SDK_INT;

if (sdk >= 21) {

return new FloatingActionButtonLollipop(this, new ShadowDelegateImpl(),

ViewUtils.DEFAULT_ANIMATOR_CREATOR);

} else if (sdk >= 14) {

return new FloatingActionButtonIcs(this, new ShadowDelegateImpl(),

ViewUtils.DEFAULT_ANIMATOR_CREATOR);

} else {

return new FloatingActionButtonGingerbread(this, new ShadowDelegateImpl(),

ViewUtils.DEFAULT_ANIMATOR_CREATOR);

}

}

上面创建FloatingActionButtonImpl每种情况都传递了ShadowDelegateImpl的一个实例,说明这个类对创建FloatingActionButtonImpl很重要,往下看这个类的源码,如下所示:private class ShadowDelegateImpl implements ShadowViewDelegate {

ShadowDelegateImpl() {

}

@Override

public float getRadius() {

return getSizeDimension() / 2f;

}

@Override

public void setShadowPadding(int left, int top, int right, int bottom) {

mShadowPadding.set(left, top, right, bottom);

setPadding(left + mImagePadding, top + mImagePadding,

right + mImagePadding, bottom + mImagePadding);

}

@Override

public void setBackgroundDrawable(Drawable background) {

FloatingActionButton.super.setBackgroundDrawable(background);

}

@Override

public boolean isCompatPaddingEnabled() {

return mCompatPadding;

}

}

这个类里有一个getRadius方法,字面意思理解就是获取半径的,终于柳暗花明,原来fab的大小(即半径)是这个方法来获取的,可以看到它调用了getSizeDimension,下面看这个getSizeDimension方法,源码如下:int getSizeDimension() {

return getSizeDimension(mSize);

}

private int getSizeDimension(@Size final int size) {

final Resources res = getResources();

switch (size) {

case SIZE_AUTO:

// If we're set to auto, grab the size from resources and refresh

final int width = ConfigurationHelper.getScreenWidthDp(res);

final int height = ConfigurationHelper.getScreenHeightDp(res);

return Math.max(width, height) < AUTO_MINI_LARGEST_SCREEN_WIDTH

? getSizeDimension(SIZE_MINI)

: getSizeDimension(SIZE_NORMAL);

case SIZE_MINI:

return res.getDimensionPixelSize(R.dimen.design_fab_size_mini);

case SIZE_NORMAL:

default:

return res.getDimensionPixelSize(R.dimen.design_fab_size_normal);

}

}

原来这里google做的控件也是通过获取资源文件dimens.xml来获取dimen值的(废话,当然是了)。终于看到这里,我才恍然大悟,我可以通过自定义这个design_fab_size_normal来实现覆盖默认的dimen值呀(因为我的布局里是设置fabSize为normal的),自定义代码如下所示:80dp

这样即完美搞定我的fab自定义大小的需求。上张效果图,如下所示:

78ba31717d0b6d67a7939e7aae748eba.png

和之前那个设置fab的固定宽高的效果比好多了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值