安卓开发笔记(三十四):Material Design框架实现优美的左侧侧滑栏
程序员文章站
2023-01-21 08:26:51
首先我们先上图: 下面是主页面的代码,activity_main.xml: 2.mainactivity.java 3.创建menu文件夹,并在文件夹下写上menu的布局文件,nav_menu.xml 4.由于我们还使用了toobar,因此在menu文件夹下同样写上toolbar.xml 5.lay ......
首先我们先上图:
下面是主页面的代码,activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <framelayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.toolbar android:id= "@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary" android:theme="@style/themeoverlay.appcompat.dark.actionbar" android:popuptheme="@style/themeoverlay.appcompat.light" > </android.support.v7.widget.toolbar> </framelayout> <android.support.design.widget.navigationview android:id="@+id/nav_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" app:menu="@menu/nav_menu" app:headerlayout="@layout/nav_header" /> </android.support.v4.widget.drawerlayout>
2.mainactivity.java
import android.support.v4.view.gravitycompat; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbar; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.gridlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import android.widget.toast; import java.util.arraylist; import java.util.list; import java.util.random; public class mainactivity extends appcompatactivity { private drawerlayout mdrawerlayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); mdrawerlayout=(drawerlayout)findviewbyid(r.id.drawer_layout); actionbar actionbar=getsupportactionbar(); if(actionbar!=null) { actionbar.setdisplayhomeasupenabled(true); //actionbar.sethomeasupindicator(r.mipmap.ic_menu); } } public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.toolbar,menu); return true; } public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()){ case r.id.backup: toast.maketext(this,"你点击了上传",toast.length_short).show(); break; case r.id.delete: toast.maketext(this,"你点击了删除",toast.length_short).show(); break; case r.id.settings: toast.maketext(this,"你点击了设置",toast.length_short).show(); break; case r.id.home: mdrawerlayout.opendrawer(gravitycompat.start); break; default: } return true; } }
3.创建menu文件夹,并在文件夹下写上menu的布局文件,nav_menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkablebehavior="single" > </group> <item android:id="@+id/nac_call" android:title="给她打电话" android:icon="@mipmap/call" /> <item android:id="@+id/nac_friends" android:title="抱抱" android:icon="@mipmap/friends" /> <item android:id="@+id/nac_location" android:title="定位" android:icon="@mipmap/iconlocation" /> <item android:id="@+id/nac_mail" android:title="发邮件" android:icon="@mipmap/mail" /> <item android:id="@+id/nac_task" android:title="养宠物注意事项" android:icon="@mipmap/tasks" /> <item android:id="@+id/nac_jihua" android:title="旅行计划" android:icon="@mipmap/jihua" /> </menu>
4.由于我们还使用了toobar,因此在menu文件夹下同样写上toolbar.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/backup" android:icon="@drawable/ic_backup" android:title="backup" app:showasaction="always" /> <item android:id="@+id/delete" android:icon="@drawable/ic_delete" android:title="delete" app:showasaction="ifroom" /> <item android:id="@+id/settings" android:icon="@drawable/settings" android:title="settings" app:showasaction="never" /> </menu>
5.layout文件夹下的nav_header.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="180dp" android:padding="10dp" android:background="@drawable/bg" > <de.hdodenhof.circleimageview.circleimageview android:id="@+id/icon_image" android:layout_width="70dp" android:layout_height="70dp" android:layout_centerinparent="true" android:src="@drawable/meinv" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/username" android:layout_above="@+id/mail" android:text="我妹妹" android:textcolor="#fff" android:textsize="14sp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mail" android:layout_alignparentbottom="true" android:text="freemusic@foxmail.com" android:textcolor="#fff" android:textsize="14sp"/> </relativelayout>
这样我们的左侧侧滑栏就搞定了。
上一篇: 杏鲍菇禁忌,杏鲍菇的四大功效
下一篇: 求知某一天是星期几