我将 BottomNavigationBar(bnb) 与 Navigation(android jetpack) 一起使用。这是一个单一活动的应用程序。有3个片段。其中之一正在启动。当我执行从起始片段到任何外部的导航时,bnb 下降了一半。当我返回或在未启动片段之间执行导航时,一切正常。
活动
<androidx.constraintlayout.widget.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"
android:background="@color/colorPrimary"
tools:context=".ui.activities.MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemIconSize="@dimen/bnb_icon_size"
android:background="?attr/colorPrimary"
app:itemIconTint="@drawable/selector"
app:elevation="@dimen/margin_small"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_navigation_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constrainedWidth="true"
app:layout_constrainedHeight="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation_view"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"/>
</androidx.constraintlayout.widget.ConstraintLayout>
碎片之一
<layout 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">
<data>
<variable
name="callback"
type="(...)ui.info.InfoFragmentCallback" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/set_coord_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:fitsSystemWindows="true">
<TextView
android:id="@+id/info_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_email"
android:textColor="@color/colorWhiteOverlayTransparency45"
android:textSize="@dimen/text_size_small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/info_contact_us_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_medium"
android:gravity="center"
android:text="@string/title_contact_us"
android:textColor="@color/colorWhiteOverlayTransparency45"
android:textSize="@dimen/text_size_medium"
android:textStyle="italic"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toTopOf="@+id/info_email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/info_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_large"
android:gravity="center"
android:text="@string/app_version"
android:textColor="@color/colorWhiteOverlayTransparency45"
android:textSize="@dimen/text_size_medium"
android:textStyle="italic"
app:layout_constraintBottom_toTopOf="@+id/info_email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/info_app_rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_medium"
android:gravity="center"
android:text="@string/title_rate_app"
android:textColor="@color/colorWhiteOverlayTransparency45"
android:textSize="@dimen/text_size_medium"
android:textStyle="italic"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toTopOf="@+id/info_google_badge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/info_google_badge"
android:layout_width="wrap_content"
android:layout_height="@dimen/icon_size_large"
android:elevation="@dimen/margin_medium"
android:onClick="@{(view)->callback.onAppRateClicked()}"
android:src="@drawable/google_play_badge"
app:layout_constraintBottom_toTopOf="@+id/info_privacy_policy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/info_email"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/info_privacy_policy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_huge"
android:onClick="@{(view)->callback.onPrivacyClicked()}"
android:text="@string/privacy_policy_title"
android:textColor="@color/colorSecondary"
android:textSize="@dimen/text_size_small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
主要活动
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private NavController mNavController;
private final BottomNavigationView.OnNavigationItemReselectedListener mOnNavigationItemReselectedListener = item -> {
/*multiple touches may must nothing*/
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavController = Navigation.findNavController(this, R.id.nav_host_fragment);
bottomNavigationView = findViewById(R.id.bottom_navigation_view);
NavigationUI.setupWithNavController(bottomNavigationView, mNavController);
initBottomNavigationMenu();
}
private void initBottomNavigationMenu() {
bottomNavigationView.setOnNavigationItemReselectedListener(mOnNavigationItemReselectedListener);
}
public void performNavigation(int navId) {
mNavController.navigate(navId);
}
@Override
protected void onDestroy() {
super.onDestroy();
bottomNavigationView.setOnNavigationItemReselectedListener(null);
}
}
bnb 菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/starting_fragment"
android:icon="@drawable/ic_image"
android:title=""
app:showAsAction="ifRoom" />
<item
android:id="@+id/favorites_fragment"
android:icon="@drawable/ic_star"
android:title=""
app:showAsAction="ifRoom"/>
<item
android:id="@+id/info_fragment"
android:icon="@drawable/ic_info"
android:title=""
app:showAsAction="ifRoom"/>
</menu>
哪里可能有问题?