我正在使用Android支持设计小部件BottomNavigationView来制作我的底层导航项目。这是工作的结果:
它有2个问题,首先它不显示项目下的标题,第二个问题我希望项目填充,我不想在导航上有自由空间,我只是想让它们填充空间并划分项目之间的空间。
这是我的菜单代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_item1"
android:icon="@drawable/phone"
app:showAsAction="ifRoom"
android:title="ارتباط" />
<item
android:id="@+id/action_item2"
android:icon="@mipmap/ic_history"
app:showAsAction="ifRoom"
android:title="سوابق خرید" />
<item
android:id="@+id/action_item3"
android:icon="@drawable/sabad"
app:showAsAction="ifRoom"
android:title="سبد خرید" />
<item
android:id="@+id/action_item4"
android:icon="@drawable/market2"
app:showAsAction="ifRoom"
android:title="فروشگاه" />
</menu>
这是xml代码:
` <RelativeLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.truiton.bottomnavigation.MainActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
android:animateLayoutChanges="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
app:itemIconTint="#fff"
app:itemTextColor="#F2F2F4"
app:menu="@menu/bottom_navigation_items"/>
</RelativeLayout>`
我该如何解决这个问题?
答案 0 :(得分:2)
您的第一个问题,来自指南Bottom navigation
进行解决方案检查Android BottomNavigationView items showing without text also layout does not hiding on scroll
答案 1 :(得分:0)
两种行为(不是问题)是默认行为。如果我通过“自由空间”正确理解你,这个空间是通过移动动画来创建的,这是 BottomNavigationView 的行为。 您可以覆盖 BottomNavigationView 类的 onLayout 方法,然后您可以使用扩展标记来避免这两个。如果需要,此方法还可以为BottomNavigationView设置自定义字体系列。
public final class ExtendedBottomNavigationView extends BottomNavigationView{
private final Context context;
private Typeface fontFace = null;
public ExtendedBottomNavigationView(Context context, AttributeSet attrs){
super(context, attrs);
this.context = context;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom){
super.onLayout(changed, left, top, right, bottom);
final ViewGroup bottomMenu = (ViewGroup)getChildAt(0);
final int bottomMenuChildCount = bottomMenu.getChildCount();
BottomNavigationItemView item;
View itemTitle;
Field shiftingMode;
if(fontFace == null){
fontFace = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.VazirBold));
}
try {
//if you want to disable shiftingMode:
//shiftingMode is a private member variable so you have to get access to it like this:
shiftingMode = bottomMenu.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(bottomMenu, false);
shiftingMode.setAccessible(false);
} catch (NoSuchFieldException e){
e.printStackTrace();
} catch (IllegalAccessException e){e.printStackTrace();}
//for changing font face of item titles.
for(int i=0; i<bottomMenuChildCount; i++){
item = (BottomNavigationItemView)bottomMenu.getChildAt(i);
//this shows all titles of items
item.setChecked(true);
//every BottomNavigationItemView has two children, first is an itemIcon and second is an itemTitle
itemTitle = item.getChildAt(1);
//every itemTitle has two children, first is a smallLabel and second is a largeLabel. these two are type of AppCompatTextView
((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setTypeface(fontFace, Typeface.BOLD);
((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setTypeface(fontFace, Typeface.BOLD);
}
}
}
然后像这样使用它:
<your.package.name.ExtendedBottomNavigationView android:id="@id/bottomMenu" style="@style/bottomMenu"/>
答案 2 :(得分:0)
要启用项目下的标题,请使用此技巧。在您的BottomNavigationView中,将parametr labelVisibilityMode设置为“ labeled”。
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_gravity="bottom"
app:elevation="5dp"
**app:labelVisibilityMode="labeled"**
app:itemIconTint="@drawable/nav_item_color_state"
app:itemTextColor="@drawable/nav_item_color_state"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_navigation_menu" />
标题可见性有几种类型: