安卓笔记抽屉式布局----DrawerLayout
Posted gccbuaa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓笔记抽屉式布局----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; } } }); } }
以上是关于安卓笔记抽屉式布局----DrawerLayout的主要内容,如果未能解决你的问题,请参考以下文章
android 怎样让drawerlayout设置的侧滑菜单的内容充满屏幕