Android程序开发之使用Design包实现QQ动画侧滑效果和滑动菜单导航
google在2015的io大会上,给我们带来了更加详细的material design设计规范,同时,也给我们带来了全新的android design support library,在这个support库里面,google给我们提供了更加规范的md设计风格的控件。最重要的是,android design support library的兼容性更广,直接可以向下兼容到android 2.2。这不得不说是一个良心之作。
使用方法很简单,只需要添加一句依赖
compile 'com.android.support:design:24.0.0'
先用tablayout和snackbar以及navigation实现qq侧滑动画效果和滑动
项目已经同步至:https://github.com/nanchen2251/designnavigation-and-tablayout
上个整体效果图
首先带来的是tablayout
tab滑动切换view并不是一个新的概念,但是google却是第一次在support库中提供了完整的支持,
而且,design library的tablayout 既实现了固定的选项卡 - view的宽度平均分配,
也实现了可滚动的选项卡 - view宽度不固定同时可以横向滚动。选项卡可以在程序中动态添加,
但大部分时间我们都不会这样用,通常滑动布局都会和viewpager配合起来使用,所以,我们需要viewpager来帮忙:
通过一句话setupwithviewpager,我们就把viewpager和tablayout结合了起来。
上个运行图:
看代码
首先是主页面的xml文件
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.nanchen.designtablayoutdemo.mainactivity"> <android.support.design.widget.tablayout android:layout_width="match_parent" app:tabbackground="@color/colorprimarydark" app:tabtextcolor="@android:color/white" app:tabselectedtextcolor="#04b4ae" android:layout_height="wrap_content" android:id="@+id/main_tab_layout"/> <android.support.v4.view.viewpager android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/main_vp"/> </linearlayout>
其中由于必须tablayout控件是design包下的,所以必须加上
xmlns:app="http://schemas.android.com/apk/res-auto"<br><br>这样才可以使用自定义它的属性。它的属性有很多,这样简单使用几个,更多的大家可以去尝试。<br>这里,我只滚动了三个,若是你的app tab标签多的话,
app:tabmode="scrollable" 必须指定这个属性,不然你会发现看不到,如果标签数少,还是建议设置为固定的,那样才会等分。
<br><br>其次是fragment的xml文件<br><br> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.nanchen.designtablayoutdemo.blankfragment"> <!-- todo: update blank fragment layout --> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:textsize="30sp" android:gravity="center" android:id="@+id/fg_text" android:text="@string/hello_blank_fragment"/> </framelayout>
mainactivity.java
package com.example.nanchen.designtablayoutdemo; import android.os.bundle; import android.support.design.widget.tablayout; import android.support.v4.view.viewpager; import android.support.v7.app.appcompatactivity; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewpager vp = (viewpager) findviewbyid(r.id.main_vp); tablayout tablayout = (tablayout) findviewbyid(r.id.main_tab_layout); list<string> list = new arraylist<>(); for (int i = 1; i < 4; i++) { // list.add(string.format(locale.china,"第02d%页",i)); list.add("第"+i+"页"); } vp.setadapter(new myadapter(getsupportfragmentmanager(),list)); tablayout.setupwithviewpager(vp); } }
fragment用于存放下面的数据
package com.example.nanchen.designtablayoutdemo; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; /** * a simple {@link fragment} subclass. */ public class blankfragment extends fragment { public blankfragment() { // required empty public constructor } public static blankfragment newinstance(string text) { bundle args = new bundle(); args.putstring("text",text); blankfragment fragment = new blankfragment(); fragment.setarguments(args); return fragment; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate the layout for this fragment return inflater.inflate(r.layout.fragment_blank, container, false); } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); textview text = (textview) view.findviewbyid(r.id.fg_text); string str = getarguments().getstring("text"); text.settext(str); } }
需要一个pageradapter
自定义一个。
package com.example.nanchen.designtablayoutdemo; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import java.util.list; /** * created by 南尘 on 2016/7/12. */ public class myadapter extends fragmentpageradapter { private list<string> list; public myadapter(fragmentmanager fm, list<string> list) { super(fm); this.list = list; } @override public fragment getitem(int position) { return blankfragment.newinstance(list.get(position)); } @override public int getcount() { return list.size(); } @override public charsequence getpagetitle(int position) { return list.get(position); } }
2)再来看看如何使用navigation和drawerlayout实现侧滑功能的动画演示
前面讲解了design包下的tablayout的使用,下面将带来navagationview和drawlayout以及toolbar的联动。
navigationview 通过提供抽屉导航所需的框架让实现更简单,同时它还能够直接通过菜单资源文件直接生成导航元素。把navigationview作为drawerlayout的内容视图来使用。navigationview处理好了和状态栏的关系,可以确保navigationview在api21+设备上正确的和状态栏交互。
以下代码在前面代码的基础上修改。
mainactivity.java
package com.example.nanchen.designtablayoutdemo; import android.os.bundle; import android.support.design.widget.navigationview; import android.support.design.widget.tablayout; import android.support.v4.view.gravitycompat; import android.support.v4.view.viewpager; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.menuitem; import android.widget.toast; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { private drawerlayout drawerlayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); viewpager vp = (viewpager) findviewbyid(r.id.main_vp); tablayout tablayout = (tablayout) findviewbyid(r.id.main_tab_layout); toolbar toolbar = (toolbar) findviewbyid(r.id.main_toolbar); drawerlayout = (drawerlayout) findviewbyid(r.id.main_drawerlayout); //设置toolbar和drawerlayout实现动画和联动 this.setsupportactionbar(toolbar); actionbardrawertoggle toggle = new actionbardrawertoggle(this,drawerlayout,toolbar,0,0); drawerlayout.adddrawerlistener(toggle);//设置监听 toggle.syncstate();//加上同步 getsupportactionbar().setdefaultdisplayhomeasupenabled(true); list<string> list = new arraylist<>(); for (int i = 1; i < 5; i++) { // list.add(string.format(locale.china,"第02d%页",i)); list.add("第"+i+"页"); } vp.setadapter(new myadapter(getsupportfragmentmanager(),list)); tablayout.setupwithviewpager(vp); navigationview navigationview = (navigationview) findviewbyid(r.id.main_navigation); navigationview.setnavigationitemselectedlistener(this); } @override public boolean onnavigationitemselected(menuitem item) { switch (item.getitemid()) { case r.id.menu_item1: toast.maketext(this,"你点击了菜单1",toast.length_short).show(); break; case r.id.menu_item2: toast.maketext(this,"你点击了菜单2",toast.length_short).show(); break; case r.id.menu_item3: toast.maketext(this,"你点击了菜单3",toast.length_short).show(); break; case r.id.menu_item4: toast.maketext(this,"你点击了菜单4",toast.length_short).show(); break; } drawerlayout.closedrawer(gravitycompat.start); return true; } }
主布局
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.nanchen.designtablayoutdemo.mainactivity"> <!--toolbar,actionbar的替代品--> <android.support.v7.widget.toolbar android:id="@+id/main_toolbar" app:theme="@style/themeoverlay.appcompat.dark.actionbar" android:background="@color/colorprimary" android:layout_width="match_parent" android:layout_height="?actionbarsize"/> <android.support.v4.widget.drawerlayout android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/main_drawerlayout" android:layout_weight="1"> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.tablayout android:layout_width="match_parent" app:tabbackground="@color/colorprimarydark" app:tabtextcolor="@android:color/white" app:tabselectedtextcolor="#04b4ae" android:layout_height="wrap_content" android:id="@+id/main_tab_layout"/> <android.support.v4.view.viewpager android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/main_vp"/> </linearlayout> <android.support.design.widget.navigationview app:menu="@menu/navigation" android:id="@+id/main_navigation" android:layout_gravity="start" app:headerlayout="@layout/header" app:itemicontint="@color/navigation_selector" app:itemtextcolor="@color/navigation_selector" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.design.widget.navigationview> </android.support.v4.widget.drawerlayout> </linearlayout>
navigation.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkablebehavior="single"> <item android:id="@+id/single_1" android:title="单选1" android:icon="@mipmap/ic_launcher"/> <item android:id="@+id/single_2" android:title="单选2" android:icon="@mipmap/ic_launcher"/> <item android:id="@+id/single_3" android:title="单选3" android:icon="@mipmap/ic_launcher"/> </group> <item android:id="@+id/menu_item1" android:title="菜单1" android:icon="@mipmap/ic_launcher"/> <item android:id="@+id/menu_item2" android:title="菜单2" android:icon="@mipmap/ic_launcher"/> <item android:id="@+id/menu_item3" android:title="菜单3" android:icon="@mipmap/ic_launcher"/> <item android:id="@+id/menu_item4" android:title="菜单4" android:icon="@mipmap/ic_launcher"/> </menu>
header.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:gravity="center" android:background="@drawable/header_bg" android:layout_height="200dp"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文本"/> </linearlayout>
header.bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:type="linear" android:angle="45" android:startcolor="#143e52" android:endcolor="#06a0ff"/> </shape>
navigation_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#fa08ca" android:state_checked="true"/> <item android:color="#6b6a6a" android:state_checked="false"/> </selector>
以上所述是小编给大家介绍的android程序开发之使用design包实现qq动画侧滑效果和滑动菜单导航,希望对大家有所帮助
上一篇: 2018.5.23 I/O 流