动态修改BottomNavigationBar的大小

android系统自带的BottomNavigationBar用起来是很方便,就是固定了大小,即使在xml里面配置了高度。当你设置的高度大于56dp,你会发现底部有两层,后面一层白底的布局比BottomNavigationBar高,露出来了。在看了BottomNavigationBar源码后发现在加载它的布局的时候,把inflate出来的view添加到BottomNavigationBar上了。

 private void init() {

        //        MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) getContext().getResources().getDimension(R.dimen.bottom_navigation_padded_height)));
        //        marginParams.setMargins(0, (int) getContext().getResources().getDimension(R.dimen.bottom_navigation_top_margin_correction), 0, 0);

        setLayoutParams(new ViewGroup.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)));
        
        LayoutInflater inflater = LayoutInflater.from(getContext());
        //这里把this作为root,并且设置了attachToRoot为true
        View parentView = inflater.inflate(R.layout.bottom_navigation_bar_container, this, true);
        mBackgroundOverlay = parentView.findViewById(R.id.bottom_navigation_bar_overLay);
        //这个就是加载出来的布局文件,是一个FrameLayout
        mContainer = parentView.findViewById(R.id.bottom_navigation_bar_container);
        //这里是放用户添加的的导航item,是一个LinearLayout
        mTabContainer = parentView.findViewById(R.id.bottom_navigation_bar_item_container);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setOutlineProvider(ViewOutlineProvider.BOUNDS);
        } else {
            //to do
        }

        ViewCompat.setElevation(this, mElevation);
        setClipToPadding(false);
    }

我们知道,如果attachToRoot了,返回的就是root布局,这里也告诉我们在inflate列表类View的item时候不能调用

inflater.inflate($layoutId$,root);因为该方法会调用inflater.inflate($layoutId$,root,root != null);即inflater.inflate($layoutId$,root,true);

会把root作为结果返回。这自然不是你想要的。要么调用inflater.inflate($layoutId$,null);非要带上root的时候,调用inflater.inflate($layoutId$,root,false);

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");

            final Context inflaterContext = mContext;
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context) mConstructorArgs[0];
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值