在这里使用的官网API的DrawerLayout实现的,也可以SlidingMenu来实现,我基本是参照官网的介绍步骤的实现。代码略有不同
一、 实现效果图
二、实现源码
main_navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_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" >
<include layout="@layout/activity_main_activity_t"/> <!--这里xml是主界面的-->
</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff"/>
</android.support.v4.widget.DrawerLayout>
java代码:
setContentView(R.layout.main_navigation_menu);
//初始化侧边栏
private void initDrawer(){
mDrawerLayout=(DrawerLayout)findViewById(R.id.main_drawer_layout);
left_drawer_lv=(ListView)findViewById(R.id.left_drawer);
left_drawer_lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"one","two","three"}));
left_drawer_lv.setOnItemClickListener(new LeftDrawerItemClickListener());
}
1 创建布局文件xml,布局根元素是
DrawerLayout
- 在DrawerLayout中包含两个子元素,第一个是FrameLayout作为主界面,可以在FrameLayout添加之前创建的布局文件
- 第二个子元素为ListView,作为侧边栏的菜单
不用翻墙就可以看android api的地址:http://wear.techbrood.com/training/implementing-navigation/nav-drawer.html