首页 >> 大全

安卓Bottom Navigation Activity的自定义使用

2023-07-23 大全 26 作者:考证青年

初始底部导航

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

初始化的项目结构如下:

自定义底部导航栏

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

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

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

此时我们就会发现,文件报错 'ding'

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

// 修改前
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;}@Overridepublic 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;}@Overridepublic void onDestroyView() {super.onDestroyView();binding = null;}
}

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

.xml文件(@/需要在文件夹中的.xml中进行配置,@/需要在文件夹中添加图标)


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



此时又会产生报错: ''

我们找到报错的行

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

修改如下:

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

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

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了