安卓Bottom Navigation Activity的自定义使用,即常见错误的处理

初始化底部导航栏  

        首先我们建立一个带有Bottom Navigation Activity项目,新建项目时直接选择Bottom Navigation Activity即可,也可以右键新建Bottom Navigation Activity活动。

初始化的项目结构如下:

  • java中ui文件夹中包含的三个文件夹即为底部导航栏的三个模块,xxxFragment为碎片文件,xxxViewModel为数据视图文件;
  • MainActivity是该底部导航栏的活动,三个碎片链接在该活动中;
  • layout文件夹中三个fragment_xxx.xml文件即为三个模块的布局文件,activity_main.xml文件即为MainActivity活动的布局文件;
  • menu文件夹中的bottom_nav_menu.xml为菜单配置文件(如果要增加模块需要在此处进行添加);
  • navigation文件夹中的mobile_navigation.xml为碎片与底部导航栏链接配置文件(如果要增加模块此处也需要进行添加);
  • values文件夹中的文件则为一些基本配置文件,如colors.xml为全局颜色配置。

自定义底部导航栏

        在上面初始化导航栏的基础上,我们进行自定义修改。

        首先,我们将ui文件夹中的文件夹级文件重命名(Shift+F6),例如:将dashboard改为myself

 

        修改ViewModel文件时会有如下弹窗,我们选中点击OK即可。

         此时我们就会发现,myselfFragment文件报错Cannot resolve symbol 'FragmentDashboardBinding'

         这个问题来源于我们没有修改该文件的FragmentXxxBinding,修改如下:

// 修改前
public class MyselfFragment extends Fragment {

    private FragmentDashboardBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        MyselfViewModel myselfViewModel =
                new ViewModelProvider(this).get(MyselfViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textDashboard;
        myselfViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}

// 修改后
public class MyselfFragment extends Fragment {

    private FragmentMyselfBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        MyselfViewModel myselfViewModel =
                new ViewModelProvider(this).get(MyselfViewModel.class);

        binding = FragmentMyselfBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textDashboard;
        myselfViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}

接下来我们修改menu和navigation文件夹中的文件

bottom_nav_menu.xml文件(@string/title_myself需要在values文件夹中的strings.xml中进行配置,@drawable/ic_myself_black_24dp需要在drawable文件夹中添加图标)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_myself"
        android:icon="@drawable/ic_myself_black_24dp"
        android:title="@string/title_myself" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />

</menu>

mobile_navigation.xml文件修改如下:id属性用于在MainActivity中链接,name属性用于链接Fragment文件,label为标题,layout为布局文件。

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.example.bottom_nav_test.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_myself"
        android:name="com.example.bottom_nav_test.ui.myself.MyselfFragment"
        android:label="@string/title_Myself"
        tools:layout="@layout/fragment_myself" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.example.bottom_nav_test.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
</navigation>

此时MainActivity又会产生报错:Cannot resolve symbol 'navigation_dashboard'

我们找到报错的行

由于我们修改了mobile_navigation.xml的id属性,所以MainActivity中也需要进行修改。

修改如下:

AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_myself,             
                R.id.navigation_notifications).build();

 此时就启动成功了:(中间的标题在xxxViewModel.java中修改)

 

BottomNavigationView是Android官方提供的一个用于底部导航栏的控件,我们可以通过自定义样式来改变其外观。 首先,在res目录下的values文件夹中创建一个新的styles.xml文件。这个文件将用于定义BottomNavigationView的样式。 然后,在styles.xml文件中添加一个新的style,如下所示: ``` <style name="BottomNavigationViewStyle" parent="Widget.Design.BottomNavigationView"> <item name="android:background">@color/colorPrimary</item> <item name="android:itemIconTint">@color/selector_bottom_navigation_icon</item> <item name="android:itemTextColor">@color/selector_bottom_navigation_text</item> <item name="itemTextAppearanceActive">@style/BottomNavigationTextAppearanceActive</item> <item name="itemTextAppearanceInactive">@style/BottomNavigationTextAppearanceInactive</item> </style> ``` 上述代码中,我们定义了BottomNavigationView的背景颜色为colorPrimary,图标和文字的颜色使用了selector_bottom_navigation_icon和selector_bottom_navigation_text文件中的颜色选择器。同时,我们还定义了活动状态和非活动状态下的文字样式为BottomNavigationTextAppearanceActive和BottomNavigationTextAppearanceInactive。 最后,在布局文件中使用我们自定义BottomNavigationView样式,如下所示: ``` <android.support.design.widget.BottomNavigationView android:id="@+id/bottomNavigationView" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/BottomNavigationViewStyle" app:menu="@menu/bottom_navigation_menu" /> ``` 以上就是对BottomNavigationView自定义样式的简单介绍。通过自定义样式,我们可以轻松地改变BottomNavigationView的外观,从而适应我们的项目需求。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力敲代码呀~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值