Android Drawerlayout实现侧滑菜单效果
程序员文章站
2023-12-12 17:56:16
本文实例为大家分享了drawerlayout侧滑菜单的具体代码,供大家参考,具体内容如下
1、drawerlayout的xml布局
本文实例为大家分享了drawerlayout侧滑菜单的具体代码,供大家参考,具体内容如下
1、drawerlayout的xml布局
<?xml version="1.0" encoding="utf-8"?> <relativelayout 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="animtest.com.example.e531.drawerlayout_demo.mainactivity"> <!--侧拉菜单--> <android.support.v4.widget.drawerlayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drlayout"> <!--主页面的布局--> <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fcontent"> </framelayout> <!--拉菜单的布局 android:layout_gravity="start" 菜单在左边 end 菜单在右边 --> <relativelayout android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="end" android:background="#ffff00" android:id="@+id/rel"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:id="@+id/img"/> <listview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv" android:layout_below="@id/img"></listview> </relativelayout> </android.support.v4.widget.drawerlayout> </relativelayout>
2、在activity中的具体使用方法
package animtest.com.example.e531.drawerlayout_demo; import android.support.v4.widget.drawerlayout; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; import android.widget.relativelayout; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { private listview listview; private drawerlayout drawerlayout; private relativelayout rel; private list<string> datas; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //查找组件 drawerlayout = (drawerlayout) findviewbyid(r.id.drlayout); listview = (listview) findviewbyid(r.id.lv); rel = (relativelayout) findviewbyid(r.id.rel); //构造数据 datas = new arraylist<string>(); for(int i=0;i<10;i++){ datas.add("菜单"+i); } //设置数组适配器 arrayadapter<string> adapter=new arrayadapter<string>(this,android.r.layout.simple_list_item_1, datas); listview.setadapter(adapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { //关闭侧滑菜单 drawerlayout.closedrawer(rel); //drawerlayout.opendrawer(rel);//打开侧滑菜单 //设置参数 contentfragment contentfragment=new contentfragment(); bundle bundle=new bundle(); bundle.putstring("value",datas.get(position)); contentfragment.setarguments(bundle); //切换fragment getsupportfragmentmanager().begintransaction().replace(r.id.fcontent,contentfragment).commit(); } }); drawerlayout.setdrawerlistener(new drawerlayout.drawerlistener() { //slideoffset:侧滑菜单 宽度的像素值 @override public void ondrawerslide(view drawerview, float slideoffset) { } @override public void ondraweropened(view drawerview) { log.d("zzz","open"); } @override public void ondrawerclosed(view drawerview) { log.d("zzz","close"); } @override public void ondrawerstatechanged(int newstate) { log.d("zzz","状态改变:"+newstate); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。