android DrawerLayout侧滑菜单实现
2015-11-15 11:40
615 查看
在网上看到很多实现侧滑菜单的例子,但是都比较麻烦。 也找了一个比较简单的实现,用到DrawerLayout。只需要在xml文件中配置好,在Activity中就可以直接使用了。
xml文件如下
xml文件如下
<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" > <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="open" /> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" /> </FrameLayout> <!-- The left drawer --> <ListView android:id="@+id/left_list" android:layout_width="150dp" android:layout_height="match_parent" android:background="#ffffff" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout> </RelativeLayout>Activity如下
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; public class Main extends Activity { private DrawerLayout mDrawerLayout; private ListView left_list; private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); left_list = (ListView)this.findViewById(R.id.left_list); text = (TextView)this.findViewById(R.id.text); List<String> list = new ArrayList<String>(); list.add("abcedfg"); list.add("hijklmn"); list.add("opqrst"); list.add("uvwxyz"); left_list.setAdapter(new ArrayAdapter<>(left_list.getContext(), android.R.layout.simple_list_item_1, list)); left_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub text.setText("hello" + arg2); System.out.println("hello"); } }); Button button = (Button) findViewById(R.id.btn); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mDrawerLayout.openDrawer(Gravity.LEFT); } }); } }这样,就能很简单的实现侧滑菜单的效果了。
相关文章推荐
- Android手机获取屏幕分辨率(屏幕高度和宽度)
- Android Studio的使用(一)--显示行号、快速查找方法源
- Android 生命周期笔记
- Android——导入包出现PackageDebug问题
- Android基础——ContentProvider使用
- android 自带的下拉刷新控件,含Demo(SwipeRefreshLayout控件)
- Android中的各种XML
- android布局第一练
- <shape> android 各属性介绍
- Android 完全掌握Service
- android侧滑菜单功能代码
- 从零学Android(八)、Android资源类型之Drawable资源
- Android 路径
- Androd tools介绍
- Android Studio当鼠标放在函数上时doc提示
- android96 内存创建图片副本,画画板
- Android广播
- Android之将字符串转换为MD5码
- Android 知识点小结
- 字符串相关