Google发布了带有BottomNavigationView的新支持库v25
有什么办法可以删除物品标签?
layout_marginBottom="-16dp"
将删除此填充,但会使所有视图变小。
android:paddingTop="8dp" android:layout_marginBottom="-8dp"
这可以防止因小酒吧
Answers:
不幸的是,BottomNavigationView的第一个版本有很多限制。现在,您不能仅使用支持设计API来删除标题。因此,要解决此限制(而Google并未实现此限制),则可以执行以下操作(使用反射):
1.在bottom_navigation_menu.xml文件中将标题设置为空。
2.扩展BottomNavigationView:
public class MyBottomNavigationView extends BottomNavigationView {
public MyBottomNavigationView(Context context, AttributeSet attrs) {
super(context, attrs);
centerMenuIcon();
}
private void centerMenuIcon() {
BottomNavigationMenuView menuView = getBottomMenuView();
if (menuView != null) {
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView menuItemView = (BottomNavigationItemView) menuView.getChildAt(i);
AppCompatImageView icon = (AppCompatImageView) menuItemView.getChildAt(0);
FrameLayout.LayoutParams params = (LayoutParams) icon.getLayoutParams();
params.gravity = Gravity.CENTER;
menuItemView.setShiftingMode(true);
}
}
}
private BottomNavigationMenuView getBottomMenuView() {
Object menuView = null;
try {
Field field = BottomNavigationView.class.getDeclaredField("mMenuView");
field.setAccessible(true);
menuView = field.get(this);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return (BottomNavigationMenuView) menuView;
}
}
3.将这个customView添加到layout.xml中
有关更多详细信息,我已经在Github上实现了
app:labelVisibilityMode="unlabeled"
1.android:title="";
在menu / abc.xml中设置
2.创建下面的使用反射的帮助器类
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.widget.AppCompatImageView;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;
import java.lang.reflect.Field;
public class BottomNavigationViewHelper {
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
item.setPadding(0, 15, 0, 0);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
3.在您的主要活动中,添加以下行:
mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(mBottomNav);
无反射方式:
private void removeTextLabel(@NonNull BottomNavigationView bottomNavigationView, @IdRes int menuItemId) {
View view = bottomNavigationView.findViewById(menuItemId);
if (view == null) return;
if (view instanceof MenuView.ItemView) {
ViewGroup viewGroup = (ViewGroup) view;
int padding = 0;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View v = viewGroup.getChildAt(i);
if (v instanceof ViewGroup) {
padding = v.getHeight();
viewGroup.removeViewAt(i);
}
}
viewGroup.setPadding(view.getPaddingLeft(), (viewGroup.getPaddingTop() + padding) / 2, view.getPaddingRight(), view.getPaddingBottom());
}
}
我想同时删除移位动画和标签,这里的解决方案都不适合我,因此这是我根据在这里学到的所有知识构建的解决方案:
public void removeLabels(@IdRes int... menuItemIds) {
getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override public boolean onPreDraw() {
getViewTreeObserver().removeOnPreDrawListener(this);
// this only needs to be calculated once for an unchecked item, it'll be the same value for all items
ViewGroup uncheckedItem = findFirstUncheckedItem(menuItemIds);
View icon = uncheckedItem.getChildAt(0);
int iconTopMargin = ((LayoutParams) uncheckedItem.getChildAt(0).getLayoutParams()).topMargin;
int desiredTopMargin = (uncheckedItem.getHeight() - uncheckedItem.getChildAt(0).getHeight()) / 2;
int itemTopPadding = desiredTopMargin - iconTopMargin;
for (int id : menuItemIds) {
ViewGroup item = findViewById(id);
// remove the label
item.removeViewAt(1);
// and then center the icon
item.setPadding(item.getPaddingLeft(), itemTopPadding, item.getPaddingRight(),
item.getPaddingBottom());
}
return true;
}
});
}
@SuppressLint("RestrictedApi")
private ViewGroup findFirstUncheckedItem(@IdRes int... menuItemIds) {
BottomNavigationItemView item = findViewById(menuItemIds[0]);
int i = 1;
while (item.getItemData().isChecked()) {
item = findViewById(menuItemIds[i++]);
}
return item;
}
只需将此方法添加到您的自定义方法中,BottomNavigationView
然后调用它传递菜单项的ID即可。
我建议按照sanf0rd在他的回答中给出的方法自行实施。但是AppCompatImageView
对我不起作用。我已将其更改为ImageView
。而改变getChildAt
到findViewById
。
我也隐藏所有未选中项目的标签。
private void centerMenuIcon() {
BottomNavigationMenuView menuView = getBottomMenuView();
if (menuView != null) {
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView menuItemView = (BottomNavigationItemView) menuView.getChildAt(i);
TextView smallText = (TextView) menuItemView.findViewById(R.id.smallLabel);
smallText.setVisibility(View.INVISIBLE);
//TextView largeText = (TextView) menuItemView.findViewById(R.id.largeLabel);
ImageView icon = (ImageView) menuItemView.findViewById(R.id.icon);
FrameLayout.LayoutParams params = (LayoutParams) icon.getLayoutParams();
params.gravity = Gravity.CENTER;
menuItemView.setShiftingMode(true);
}
}
}
title
从菜单<item>
s中删除s?