宜春公司做网站,网站建设的具体方法,婚庆公司报价表,灵雀云 wordpress目录
1#xff09;左侧导航栏效果图 2#xff09;NavigationView是什么#xff1f; 3#xff09;NavigationView和Navigation搭配使用 4#xff09;NavigationView的其他方法
一、实现左侧导航栏
由于Android这边没有直接提供左侧导航栏的控件#xff0c;所以我尝试了…目录
1左侧导航栏效果图 2NavigationView是什么 3NavigationView和Navigation搭配使用 4NavigationView的其他方法
一、实现左侧导航栏
由于Android这边没有直接提供左侧导航栏的控件所以我尝试了很多方法但ui都有很多局限使用BottomNavigationView达不到如下这种效果并且我想结合Navigation来使用简化跳转以及创建fragment的流程。所以呢研究了一下NavigationView和Navigation搭配使用。 二、NavigationView是什么 NavigationView是一种用于实现应用导航功能的侧滑菜单控件。
NavigationView是Navigation在Android开发中的一个具体实现特别是用于侧边导航菜单的控件。
Navigation则是一个更广泛的概念指的是支持用户导航、进入和退出应用中不同内容片段的交互机制。它不仅限于某个特定的控件或组件而是涵盖了应用中所有与导航相关的功能和设计。 三、NavigationView和Navigation搭配使用 首先我们先创建NavigationView 1创建Menu 这里我们就先创建两个菜单项
?xml version1.0 encodingutf-8?
menu xmlns:androidhttp://schemas.android.com/apk/res/androiditemandroid:idid/backstage_datastatfragmentandroid:title首页 /!--暂时不使用数据统计的功能--itemandroid:idid/backstage_errorstatfragmentandroid:title数据统计/
/menu2创建NavigationView通过menu属性绑定刚才创建的菜单项 ?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:backgrounddrawable/dingdian_pic_background_syscom.google.android.material.navigation.NavigationViewandroid:idid/bnv_main_navigationbarandroid:layout_width400dpandroid:layout_heightmatch_parentandroid:backgroundcolor/whiteandroid:paddingHorizontal9dpapp:itemBackgroundcolor/whiteapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparentapp:menumenu/backstage_menu_setting //androidx.constraintlayout.widget.ConstraintLayout3创建Navigation 【参考这篇文章Android Jetpack(一)Navigation】https://blog.csdn.net/qq_40853919/article/details/139973342
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:backgrounddrawable/dingdian_pic_background_syscom.google.android.material.navigation.NavigationViewandroid:idid/bnv_main_navigationbarandroid:layout_width400dpandroid:layout_heightmatch_parentandroid:backgroundcolor/whiteandroid:paddingHorizontal9dpapp:itemBackgroundcolor/whiteapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparentapp:menumenu/backstage_menu_setting /androidx.fragment.app.FragmentContainerViewandroid:idid/home_fragmentcontainerviewandroid:nameandroidx.navigation.fragment.NavHostFragmentandroid:layout_width0dpandroid:layout_heightmatch_parentapp:defaultNavHosttrueapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toEndOfid/bnv_main_navigationbarapp:navGraphnavigation/backstage_nav //androidx.constraintlayout.widget.ConstraintLayoutbackstage_nav的内容
?xml version1.0 encodingutf-8?
navigation xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoandroid:idid/backstage_navapp:startDestinationid/backstage_datastatfragmentfragmentandroid:idid/backstage_datastatfragmentandroid:namecom.quyunshuo.wanandroid.home.ui.fragment.DataStatFragmentandroid:labelDataStatFragment /fragmentandroid:idid/backstage_errorstatfragmentandroid:namecom.quyunshuo.wanandroid.home.ui.fragment.ErrorStatFragmentandroid:labelErrorStatFragment /
/navigation1DataStatFragment的布局内容
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentTextViewandroid:idid/backstage_textview2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:texterrorstatandroid:textSize40dpapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent /
/androidx.constraintlayout.widget.ConstraintLayout2ErrorStatFragment的内容
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentTextViewandroid:idid/backstage_textviewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textDatastatandroid:textSize40dpapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent /
/androidx.constraintlayout.widget.ConstraintLayout注意menu中item的id值必须与Navigation.xml 中定义的fragment的id对应 不然点击没有反应 4在activity中进行导航跳转 override fun BackstageActivitySettingBinding.initView() {bnvMainNavigationbar.setNavigationItemSelectedListener{//使用navigation的方法来直接跳转因为id相同就可以直接跳转。val findNavController findNavController(homeFragmentcontainerview.id)findNavController.navigate(it.itemId)true}}运行程序点击界面会发生变化。 四、NavigationView的其他方法 1比如我们要给菜单的item增加icon那么可以使用如下这个属性 2其他属性
app:headerLayoutlayout/navigation_layoutNavigationView的头部布局指定了一个布局文件作为NavigationView的头部布局。这个头部布局通常包含一些静态信息如用户的头像和用户名。android:paddingHorizontal9dp为NavigationView设置了水平方向上的内边距即左右两边app:itemTextAppearancestyle/Menu指定了菜单项文本的外观样式。
总结NavigationView和Navigation的搭配使用Navigation简化了非常多的fragment跳转逻辑以及创建让我们使用起来更加的舒服和方便。