我正在尝试创建一个底部导航栏,该导航栏包含5个规则间隔的项目,并且图标下方没有标签。我发现并遵循了很多指导,但是与我遵循的示例不同,我的导航栏显示非常不均匀。如何使它均匀分布?
This is what it looks like now:
我的xml视图(在相对布局内):
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/navigation" />
导航xml文件:
<?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/navigation_home"
android:enabled="true"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home"
app:showAsAction="always" />
<item
android:id="@+id/navigation_look"
android:icon="@drawable/ic_find_in_page_black_24dp"
android:title="Search"
android:enabled="true"
app:showAsAction="always"/>
<item
android:id="@+id/navigation_basket"
android:enabled="true"
android:icon="@drawable/ic_shopping_basket_black_24dp"
android:title="Basket"
app:showAsAction="always"/>
<item
android:id="@+id/navigation_favourite"
android:icon="@drawable/ic_favorite_black_24dp"
android:title="Favourite"
android:enabled="true"
app:showAsAction="always"/>
<item
android:id="@+id/navigation_account"
android:enabled="true"
android:icon="@drawable/ic_person_black_24dp"
android:title="Account"
app:showAsAction="always"/>
</menu>
这是onCreate中我的CategoryActivity.java中的内容:
BottomNavigationView navigationSearch = (BottomNavigationView)
findViewById(R.id.navigation_search);
navigationSearch.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Intent main = new Intent(CategoryActivity.this, MainActivity.class);
startActivity(main);
break;
case R.id.navigation_look:
Intent search = new Intent(CategoryActivity.this, SearchActivity.class);
startActivity(search);
break;
case R.id.navigation_basket:
Intent basket = new Intent(CategoryActivity.this, BasketActivity.class);
startActivity(basket);
break;
case R.id.navigation_favourite:
Intent favourite = new Intent(CategoryActivity.this, FavouritesActivity.class);
startActivity(favourite);
break;
case R.id.navigation_account:
Intent account = new Intent(CategoryActivity.this, AccountActivity.class);
startActivity(account);
break;
}
return false;
}
});
答案 0 :(得分:0)
您可以做的是在Navigation.xml中将Title属性设置为android:title =“”,然后使用BottomNavigationViewHelper.disableShiftMode(您的BottomNavigationView)禁用移位模式
答案 1 :(得分:0)
我建议您使用片段
navigationSearch.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId())
{
case R.id.navigation_home:
selectedFragment = HomeFragment.newInstance();
break;
case R.id.navigation_look:
selectedFragment = LookFragment.newInstance();
break;
case R.id.navigation_basket:
selectedFragment = BasketFragment.newInstance();
break;
case R.id.navigation_favourite:
selectedFragment = FavouriteFragment.newInstance();
break;
case R.id.navigation_account:
selectedFragment = AccountFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout,selectedFragment);
transaction.commit();
return true;
}
});
setDefaulFragment();
private void setDefaulFragment() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout,HomeFragment.newInstance());
transaction.commit();
}
activity_category.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@drawable/gradient"
tools:context=".CategoryActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:animateLayoutChanges="true"
android:layout_above="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimaryDark"
app:itemIconTint="@drawable/nav_item_color_state"
app:itemTextColor="@drawable/nav_item_color_state"
app:menu="@menu/bottom_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
示例片段: HomeFragment.java:
public class HomeFragment extends Fragment {
View mFragment;
public static HomeFragment newInstance(){
HomeFragment homeFragment = new HomeFragment();
return homeFragment ;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mFragment = inflater.inflate(R.layout.fragment_home,container,false);
return mFragment;
}
}
示例片段: fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<TextView
android:text="Home Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
答案 2 :(得分:0)
使用水平方向的线性布局。 您可以为每个项目指定layout_weight约20。线性布局的WeightSum为100。 使用片段加载数据。
答案 3 :(得分:0)
我遇到了同样的问题,但是有4个问题,这是一种技巧,您应该在findViewById之后在BottomNavigationView上使用此方法。
@SuppressLint("RestrictedApi")
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);
// 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);
}
}
很抱歉,我无法真正更深入地解释该解决方案,因为这是我很久以前根据StackOverflow的一些建议解决的一个问题,而且我不记得有什么理论/理由需要这样做,但这在Android Oreo上对我来说效果很好,因此您可以做一些实验并找到更好的方法。