【android studio】Android Design Support Library高逼格金钱豹制作教程

更新时间:2020-03-08    来源:Google    手机版     字体:

【www.bbyears.com--Google】

最近项目不是很忙,终于有时间写博客啦,趁这几天用Android的一些新特性重新写了金钱豹的框架,先上看图,和之前的原版相比不知道大家更喜欢哪种。


前几天Devin给我开通了vpn,作为Google脑残粉,晚上默默下载了各种最新的Tools,带着我对Material Design的喜爱,正式开始啃Google官网。吐槽下技术是永远学不完的,实际上两个月前,我已经学了些Material Design,我还没来得及使用,现在那部分内容已经是被淘汰、废弃掉的技术了……

Ok,这次就讲解下NavigationView的使用,这是Android Lolipop中Android Design Support Library带来的Material design Components中推出的,此次的Components中还包含floating labels for editing text, a floating action button, snackbar, tabs。

Google的解释:

The navigation drawer can be an important focal point for identity and navigation within
your app and consistency in the design here can make a considerable difference in how easy your app is to navigate, particularly for first time users.NavigationView makes this easier by providing the framework you need for the navigation drawer as well as the ability to inflate your navigation items through a menu resource.

此前,drawertoolge+actionbar是标配,例如Google自己的GooglePlay,而这次Google的意思是navigationbar更加简易使用,只需要填充menu资源即可实现,基本不需要考虑过多的逻辑了,google已经将Actionbar和废弃了,使用Toolbar来代替,这边顺便说一句经验,也是这几天遇到的一个bug,就是要关注Google废弃的东西,要保持警惕有准备,以前我觉得废弃一个API没什么大不了的,废弃了一样能用,但是在这次最新的更新中,Google废弃了Apache的一个Api,使得我一直使用的异步请求方式不能再使用,那天也算是运气好,正好读Google官方更新内容的时候提到了这个废弃Api和解决方案,否则这个bug根本不可能给解决了,因为这个问题实在太新了,直接搜索的话,基本不可能找到解决方案,只能换一种请求方式了,这也让我更理解为什么大牛们都推荐上Google找答案的原因,这个bug要在百度找到估计还得等个半年一年或者根本找不到。
言归正传,使用navigation需要搭配Drawerlayout一起配合使用


 xmlns:android="http://schemas.android.com/apk/res/android"       
 xmlns:app="http://schemas.android.com/apk/res-auto"       
 android:layout_width="match_parent"       
 android:layout_height="match_parent"       
 android:fitsSystemWindows="true">

 android:layout_width="wrap_content"           
 android:layout_height="match_parent"           
 android:layout_gravity="start"           
 app:headerLayout="@layout/drawer_header"           
 app:menu="@menu/drawer"/>

Header的布局和普通xml是一样的,主要就是menu_drawer的编写。
主要是两种方式:1、group内部填充item 2、item可以包含字item

下面是Google官方的示例:

The simplest drawer menus will be a collection of checkable menu items:


            android:id="@+id/navigation_item_1"
        android:checked="true"
        android:icon="@drawable/ic_android"
        android:title="@string/navigation_item_1"/>
            android:id="@+id/navigation_item_2"
        android:icon="@drawable/ic_android"
        android:title="@string/navigation_item_2"/>

The checked item will appear highlighted in the navigation drawer, ensuring the user knows which navigation item is currently selected.

选中的item会自动高亮提示用户。


    android:id="@+id/navigation_subheader"
    android:title="@string/navigation_subheader">
   


                    android:id="@+id/navigation_sub_item_1"
            android:icon="@drawable/ic_android"
            android:title="@string/navigation_sub_item_1"/>
                    android:id="@+id/navigation_sub_item_2"
            android:icon="@drawable/ic_android"
            android:title="@string/navigation_sub_item_2"/>
   

监听:

OnNavigationItemSelectedListener using setNavigationItemSelectedListener(). This provides you with the MenuItem that was clicked, allowing you to handle selection events, changed the checked status, load new content, programmatically close the drawer, or any other actions you may want.
Navigationview的接口是写好的。设置setNavigationItemSelectedListener()的监听,传递参数后,会在OnNavigationItemSelected中将MenuItem传递给用户,在这边实现操作即可。

更多的内容我会在后期继续整理发布,只要时间允许,Google推出新的技术我也会第一时间学习并整理发布

本文来源:http://www.bbyears.com/seo/85908.html