单击 BottomNavView 项目后 BottomNavigationView 缩小尺寸?
BottomNavigationView reduce size after click an BottomNavView item?
当我点击底部导航项时,BottomNavigationView 尺寸会自动缩小。只有当控制权从家庭项目转移到任何其他项目时才会发生。当我第三次点击任何其他项目时,它会自动恢复到原来的大小。
。 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav_view);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.home_bottom_nav:
selectedFragment = new HomeFragment();
break;
case R.id.game_bottom_nav:
selectedFragment = new GameFragment();
// selectedFragment = GameFragment.newInstance("Para1","Para2");
break;
case R.id.even_bottom_nav:
selectedFragment = new EventFragment();
break;
case R.id.profile_bottom_nav:
selectedFragment = new ProfileFragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentLayout,selectedFragment)
.commit();
return true;
}
});
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentLayout,new HomeFragment()).commit();
XML代码
<android.support.constraint.ConstraintLayout
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=".HomeActivity">
<FrameLayout
android:id="@+id/fragmentLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="?android:attr/windowBackground"
app:itemTextAppearanceActive="@style/Widget.BottomNavigationView"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_items" />
</android.support.constraint.ConstraintLayout>
你能否交叉检查你的 BottomNavigationView:使用 wrap_content 和 alignParentBottom。尝试将 FrameLayout 高度更改为 wrap_content 并删除此行 app:layout_constraintEnd_toEndOf="parent"
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ffffff"
app:labelVisibilityMode="labeled"
app:menu="@menu/menu_buttom_navigation" />
Change your xml bottomnavigation and framlayout width 0dp. Also give bottomnavigation top constraint and height wrap_content.
<android.support.constraint.ConstraintLayout
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=".HomeActivity">
<FrameLayout
android:id="@+id/fragmentLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:itemTextAppearanceActive="@style/Widget.BottomNavigationView"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fragmentLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_items" />
</android.support.constraint.ConstraintLayout>
试试这个
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="0dp"
android:layout_height="56dp"
android:background="?android:attr/windowBackground"
app:itemTextAppearanceActive="@style/Widget.BottomNavigationView"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_nav_items"
app:itemHorizontalTranslationEnabled="false"
app:itemTextAppearanceInactive="@style/Widget.BottomNavigationView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
当我点击底部导航项时,BottomNavigationView 尺寸会自动缩小。只有当控制权从家庭项目转移到任何其他项目时才会发生。当我第三次点击任何其他项目时,它会自动恢复到原来的大小。
。 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav_view);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.home_bottom_nav:
selectedFragment = new HomeFragment();
break;
case R.id.game_bottom_nav:
selectedFragment = new GameFragment();
// selectedFragment = GameFragment.newInstance("Para1","Para2");
break;
case R.id.even_bottom_nav:
selectedFragment = new EventFragment();
break;
case R.id.profile_bottom_nav:
selectedFragment = new ProfileFragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentLayout,selectedFragment)
.commit();
return true;
}
});
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentLayout,new HomeFragment()).commit();
XML代码
<android.support.constraint.ConstraintLayout
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=".HomeActivity">
<FrameLayout
android:id="@+id/fragmentLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="?android:attr/windowBackground"
app:itemTextAppearanceActive="@style/Widget.BottomNavigationView"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_items" />
</android.support.constraint.ConstraintLayout>
你能否交叉检查你的 BottomNavigationView:使用 wrap_content 和 alignParentBottom。尝试将 FrameLayout 高度更改为 wrap_content 并删除此行 app:layout_constraintEnd_toEndOf="parent"
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ffffff"
app:labelVisibilityMode="labeled"
app:menu="@menu/menu_buttom_navigation" />
Change your xml bottomnavigation and framlayout width 0dp. Also give bottomnavigation top constraint and height wrap_content.
<android.support.constraint.ConstraintLayout
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=".HomeActivity">
<FrameLayout
android:id="@+id/fragmentLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:itemTextAppearanceActive="@style/Widget.BottomNavigationView"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fragmentLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_items" />
</android.support.constraint.ConstraintLayout>
试试这个
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="0dp"
android:layout_height="56dp"
android:background="?android:attr/windowBackground"
app:itemTextAppearanceActive="@style/Widget.BottomNavigationView"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_nav_items"
app:itemHorizontalTranslationEnabled="false"
app:itemTextAppearanceInactive="@style/Widget.BottomNavigationView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>