Java源码示例:com.google.android.material.bottomnavigation.BottomNavigationMenuView

示例1
@Override public void setAppsName(String name) {
  bottomNavigationView.getMenu()
      .getItem(4)
      .setTitle(mapAppsName(name));

  BottomNavigationMenuView menuView =
      (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
  for (int i = 0; i < menuView.getChildCount(); i++) {
    BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
    View largeLabel = item.findViewById(R.id.largeLabel);
    View smallLabel = item.findViewById(R.id.smallLabel);

    if (largeLabel instanceof TextView) {
      largeLabel.setPadding(0, 0, 0, 0);
      ((TextView) largeLabel).setEllipsize(TextUtils.TruncateAt.END);
    }

    if (smallLabel instanceof TextView) {
      smallLabel.setPadding(0, 0, 0, 0);
      ((TextView) smallLabel).setEllipsize(TextUtils.TruncateAt.END);
    }
  }
}
 
示例2
/**
 * get private mButtons in mMenuView
 *
 * @return
 */
public BottomNavigationItemView[] getBottomNavigationItemViews() {
    if (null != mButtons)
        return mButtons;
    /*
     * 1 private final BottomNavigationMenuView mMenuView;
     * 2 private BottomNavigationItemView[] mButtons;
     */
    BottomNavigationMenuView mMenuView = getBottomNavigationMenuView();
    mButtons = getField(mMenuView.getClass(), mMenuView, "buttons");
    return mButtons;
}
 
示例3
/**
 * set menu item height
 *
 * @param height in px
 */
public BottomNavigationViewInner setItemHeight(int height) {
    // 1. get mMenuView
    final BottomNavigationMenuView mMenuView = getBottomNavigationMenuView();
    // 2. set private final int mItemHeight in mMenuView
    setField(mMenuView.getClass(), mMenuView, "itemHeight", height);

    mMenuView.updateMenuView();
    return this;
}
 
示例4
/**
 * get menu item height
 *
 * @return in px
 */
public int getItemHeight() {
    // 1. get mMenuView
    final BottomNavigationMenuView mMenuView = getBottomNavigationMenuView();
    // 2. get private final int mItemHeight in mMenuView
    return getField(mMenuView.getClass(), mMenuView, "itemHeight");
}
 
示例5
private void setupUpdatesNotification() {
  BottomNavigationMenuView appsView =
      (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
  BottomNavigationItemView appsItemView =
      (BottomNavigationItemView) appsView.getChildAt(BottomNavigationMapper.APPS_POSITION);

  updatesBadge = LayoutInflater.from(this)
      .inflate(R.layout.updates_badge, appsView, false);
  updatesNumber = updatesBadge.findViewById(R.id.updates_badge);
  appsItemView.addView(updatesBadge);
  appsItemView.setVisibility(View.VISIBLE);
}
 
示例6
/**
     * change the visibility of text
     *
     * @param visibility
     */
    public BottomNavigationViewInner setTextVisibility(boolean visibility) {
        this.textVisibility = visibility;
        /*
        1. get field in this class
        private final BottomNavigationMenuView mMenuView;

        2. get field in mButtons
        private BottomNavigationItemView[] mButtons;

        3. set text size in mButtons
        private final TextView mLargeLabel
        private final TextView mSmallLabel

        4. change mItemHeight to only icon size in mMenuView
         */
        // 1. get mMenuView
        BottomNavigationMenuView mMenuView = getBottomNavigationMenuView();
        // 2. get mButtons
        BottomNavigationItemView[] mButtons = getBottomNavigationItemViews();

        // 3. change field mShiftingMode value in mButtons
        for (BottomNavigationItemView button : mButtons) {
            TextView mLargeLabel = getField(button.getClass(), button, "largeLabel");
            TextView mSmallLabel = getField(button.getClass(), button, "smallLabel");

            if (!visibility) {
                // if not record the font size, record it
                if (!visibilityTextSizeRecord && !animationRecord) {
                    visibilityTextSizeRecord = true;
                    mLargeLabelSize = mLargeLabel.getTextSize();
                    mSmallLabelSize = mSmallLabel.getTextSize();
                }

                // if not visitable, set font size to 0
                mLargeLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, 0);
                mSmallLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, 0);

            } else {
                // if not record the font size, we need do nothing.
                if (!visibilityTextSizeRecord) {
                    break;
                }

                // restore it
                mLargeLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLargeLabelSize);
                mSmallLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, mSmallLabelSize);
            }
        }

        // 4 change mItemHeight to only icon size in mMenuView
        if (!visibility) {
            // if not record mItemHeight
            if (!visibilityHeightRecord) {
                visibilityHeightRecord = true;
                mItemHeight = getItemHeight();
            }

            // change mItemHeight to only icon size in mMenuView
            // private final int mItemHeight;

            // change mItemHeight
//            System.out.println("mLargeLabel.getMeasuredHeight():" + getFontHeight(mSmallLabelSize));
            setItemHeight(mItemHeight - getFontHeight(mSmallLabelSize));

        } else {
            // if not record the mItemHeight, we need do nothing.
            if (!visibilityHeightRecord) {
                return this;
            }
            // restore mItemHeight
            setItemHeight(mItemHeight);
        }

        mMenuView.updateMenuView();
        return this;
    }
 
示例7
/**
     * enable or disable click item animation(text scale and icon move animation in no item shifting mode)
     *
     * @param enable It means the text won't scale and icon won't move when active it in no item shifting mode if false.
     */
    public BottomNavigationViewInner enableAnimation(boolean enable) {
        /*
        1. get field in this class
        private final BottomNavigationMenuView mMenuView;

        2. get field in mButtons
        private BottomNavigationItemView[] mButtons;

        3. chang mShiftAmount to 0 in mButtons
        private final int mShiftAmount

        change mScaleUpFactor and mScaleDownFactor to 1f in mButtons
        private final float mScaleUpFactor
        private final float mScaleDownFactor

        4. change label font size in mButtons
        private final TextView mLargeLabel
        private final TextView mSmallLabel
         */

        // 1. get mMenuView
        BottomNavigationMenuView mMenuView = getBottomNavigationMenuView();
        // 2. get mButtons
        BottomNavigationItemView[] mButtons = getBottomNavigationItemViews();
        // 3. change field mShiftingMode value in mButtons
        for (BottomNavigationItemView button : mButtons) {
            TextView mLargeLabel = getField(button.getClass(), button, "largeLabel");
            TextView mSmallLabel = getField(button.getClass(), button, "smallLabel");

            // if disable animation, need animationRecord the source value
            if (!enable) {
                if (!animationRecord) {
                    animationRecord = true;
                    mShiftAmount = getField(button.getClass(), button, "shiftAmount");
                    mScaleUpFactor = getField(button.getClass(), button, "scaleUpFactor");
                    mScaleDownFactor = getField(button.getClass(), button, "scaleDownFactor");

                    mLargeLabelSize = mLargeLabel.getTextSize();
                    mSmallLabelSize = mSmallLabel.getTextSize();

//                    System.out.println("mShiftAmount:" + mShiftAmount + " mScaleUpFactor:"
//                            + mScaleUpFactor + " mScaleDownFactor:" + mScaleDownFactor
//                            + " mLargeLabel:" + mLargeLabelSize + " mSmallLabel:" + mSmallLabelSize);
                }
                // disable
                setField(button.getClass(), button, "shiftAmount", 0);
                setField(button.getClass(), button, "scaleUpFactor", 1);
                setField(button.getClass(), button, "scaleDownFactor", 1);

                // let the mLargeLabel font size equal to mSmallLabel
                mLargeLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, mSmallLabelSize);

                // debug start
//                mLargeLabelSize = mLargeLabel.getTextSize();
//                System.out.println("mLargeLabel:" + mLargeLabelSize);
                // debug end

            } else {
                // haven't change the value. It means it was the first call this method. So nothing need to do.
                if (!animationRecord) {
                    return this;
                }
                // enable animation
                setField(button.getClass(), button, "shiftAmount", mShiftAmount);
                setField(button.getClass(), button, "scaleUpFactor", mScaleUpFactor);
                setField(button.getClass(), button, "scaleDownFactor", mScaleDownFactor);
                // restore
                mLargeLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLargeLabelSize);
            }
        }
        mMenuView.updateMenuView();
        return this;
    }
 
示例8
/**
 * get private mMenuView
 *
 * @return
 */
public BottomNavigationMenuView getBottomNavigationMenuView() {
    if (null == mMenuView)
        mMenuView = getField(BottomNavigationView.class, this, "menuView");
    return mMenuView;
}
 
示例9
@Override
public BottomNavigationMenuView getBottomNavigationMenuView() {
    return super.getBottomNavigationMenuView();
}