首页 > 代码库 > 【安卓笔记】抽屉式布局----DrawerLayout
【安卓笔记】抽屉式布局----DrawerLayout
效果如下:
DrawerLayout来自support.v4包,所以不用考虑兼容性问题。其次,这种布局类似风靡一时的侧滑菜单,但是比侧滑菜单轻巧许多。
下面介绍这种布局的使用方式。
1.在你的项目中导入support.v4包。
2.编辑一个布局,根节点为android.support.v4.widget.DrawerLayout,此节点下只允许有两个子节点,第一个为将来主页面的内容,第二个节点即为"抽屉"内容,通常是一个ListView。比如:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈" android:textColor="#000" android:textSize="20sp" /> <Button android:id="@+id/but_open" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开启抽屉" /> </LinearLayout> <!-- 一定要加 layout_gravity="start" --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#111" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout>3.编写代码,监听相关事件:
package com.example.drawerlayoutdemo1; import android.app.Activity; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; public class MainActivity extends Activity { protected static final String TAG = "MainActivity"; private DrawerLayout mDrawerLayout = null; private ListView lv = null; private Button but_open = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); lv = (ListView) findViewById(R.id.left_drawer); but_open = (Button) findViewById(R.id.but_open); but_open.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mDrawerLayout.openDrawer(Gravity.LEFT); } }); lv.setAdapter(new ArrayAdapter<String>(this,R.layout.test,R.id.tv_item,new String[]{"条目1","条目2","条目3"})); mDrawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() { @Override public void onDrawerClosed(View drawerView) { Log.i(TAG,"抽屉关闭了..."); } @Override public void onDrawerOpened(View drawerView) { Log.i(TAG,"抽屉打开了..."); } @Override public void onDrawerSlide(View drawerView, float slideOffset) { Log.i(TAG,"抽屉在滑动..."); } @Override public void onDrawerStateChanged(int newState) { switch (newState) { case DrawerLayout.STATE_DRAGGING: Log.i(TAG,"拖动状态"); break; case DrawerLayout.STATE_IDLE: Log.i(TAG,"静止状态"); break; case DrawerLayout.STATE_SETTLING: Log.i(TAG,"设置状态"); break; default: break; } } }); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0: Log.i(TAG,"条目1被点击"); break; case 1: Log.i(TAG,"条目2被点击"); break; case 2: Log.i(TAG,"条目3被点击"); break; } } }); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。