Android Drawerlayout实现侧滑菜单效果
2017-10-24 16:10
671 查看
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); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Android侧滑菜单控件DrawerLayout使用详解
- android侧滑菜单控件DrawerLayout使用方法详解
- Android使用DrawerLayout实现双向侧滑菜单
- Android中DrawerLayout实现侧滑菜单效果
- Android使用DrawerLayout实现侧滑菜单效果
- Android之侧滑菜单DrawerLayout的使用介绍
- Android组件DrawerLayout仿网易新闻v4.4侧滑菜单
- Android使用DrawerLayout实现仿QQ双向侧滑菜单
- Android实现原生侧滑菜单的超简单方式
- Android使用DrawerLayout仿QQ6.0双侧滑菜单
相关文章推荐
- Android DrawerLayout+ToolBar+NavigationView实现侧滑菜单效果,沉浸式状态栏
- Android使用DrawerLayout实现侧滑菜单效果
- Android中DrawerLayout实现侧滑菜单效果
- android 侧滑菜单效果----DrawerLayout(抽屉导航)
- Android Toolbar+DrawerLayout+PagerSlidingTabStrip实现仿csdn侧滑菜单
- Android:使用Toolbar + DrawerLayout快速实现菜单侧滑
- Android DrawerLayout+NavigationView布局实现左右两边侧滑菜单
- Android使用DrawerLayout实现仿QQ双向侧滑菜单
- android组件之DrawerLayout(抽屉导航)-- 侧滑菜单效果
- android组件之DrawerLayout(抽屉导航)-- 侧滑菜单效果
- android组件之DrawerLayout -- Android自带侧滑菜单效果
- 用Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效果
- [AndroidUI]使用DrawerLayout实现侧滑菜单
- Android Material Design系列之使用Toolbar + DrawerLayout实现高侧滑菜单
- Android使用DrawerLayout和ToolBar实现仿知乎侧滑菜单
- Android 使用DrawerLayout实现抽屉效果的导航菜单
- android 使用DrawerLayout实现侧滑菜单
- android DrawerLayout实现仿QQ侧滑菜单
- android 使用ViewDragHelper轻松实现DrawerLayout和SlidMenu侧滑效果
- android开发游记:DrawerLayout 实现抽屉效果的导航菜单