如何管理菜单图标和文本之间的间隙,因为它们在被单击后重叠,对于底部导航视图?



我增加了">菜单图标"的大小,这就是为什么它与">菜单文本"重叠的原因。

因此,为此,我使用以下方法"menuAlignment(("管理了它们之间的差距,但是在单击"菜单项"后,"菜单图标"和">菜单文本"之间的差距又回到了以前的状态,即重叠,请帮助我。

我的代码如下:

public void menuAlignment()
{
for (int i = 0; i < menuView.getChildCount(); i++) {
final View iconView = menuView.getChildAt(i).findViewById(android.support.design.R.id.icon);
final FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) iconView.getLayoutParams();
final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
// set your height here
layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 27, displayMetrics);
// set your width here
layoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 28, displayMetrics);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
layoutParams.setMargins(0,0,0,200);
iconView.setLayoutParams(layoutParams);
//  iconView.setPadding(0, 0, 0, 10);
}
}

">导航点击监听器"的代码:-

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_jokes_word:
loadFragment(new JokesFragment());
menuAlignment();
return true;
case R.id.navigation_jokes_images:
loadFragment(new ImageJokesFragment());
menuAlignment();
return true;
}
return false;
}
};

导航.xml [菜单.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_word"
android:icon="@drawable/digital"
android:title="@string/title_jokes_word"
app:showAsAction="ifRoom" />
<item
android:id="@+id/navigation_images"
android:icon="@drawable/images"
android:title="@string/title_jokes_images" />

</menu>

我的活动截图:

底部导航视图的菜单

菜单项视图

<?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_home"
android:icon="@drawable/ic_home_blue_48dp"
android:title="Home" />
<item
android:id="@+id/action_menu"
android:icon="@drawable/ic_apps_black_24dp"
android:title="Menu"
/>
<item
android:id="@+id/action_msg"
android:icon="@drawable/ic_chat_black_24dp"
android:title="Message Inbox"
/>
</menu>

单击侦听器

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
Fragment selectedFragment = null;
switch (item.getItemId())
{
case R.id.action_home:
/*hide extra toolbars*/
hideViews();
/*hide back button*/
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
selectedFragment = new TimelineFragment();
break;
case R.id.action_menu:
hideViews();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
selectedFragment = new HomeFragment();
break;
case R.id.action_msg:
hideViews();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
selectedFragment = new ChatFragment();
break;
}

最新更新