第一种方法::::
activity_main布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:id="@+id/dl" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.myapplication.MainActivity"> <!--主页面布局--> <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull_refresh_list" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 侧滑区域的布局--> <ListView android:id="@+id/dl_lv" android:layout_width="320dp" android:layout_height="match_parent" android:layout_gravity="start" android:entries="@array/goodsType" android:background="#fff" /> </android.support.v4.widget.DrawerLayout>
strings:
<resources> <string name="app_name">1511B商城</string> <string-array name="goodsType"> <item>新闻</item> <item>关注</item> <item>动态</item> <item>设置</item> </string-array> </resources>
MainActivity代码:
DrawerLayout dl = (DrawerLayout) findViewById(R.id.dl); ListView dl_lv = (ListView) findViewById(R.id.dl_lv); //手动关闭侧滑菜单 dl.closeDrawer(dl_lv); //给drawerLayout添加监听 dl.setDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(View drawerView, float slideOffset) { } @Override public void onDrawerOpened(View drawerView) { Log.d("wyy","侧拉出来了!"); } @Override public void onDrawerClosed(View drawerView) { Log.d("wyy","侧拉关闭了!"); } @Override public void onDrawerStateChanged(int newState) { } });
第二种方法::::::
1.在主代码页面写
DrawerLayout dl;导包
代码如下:
package com.example.myapplication; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { DrawerLayout dl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
2.布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.myapplication.MainActivity"> <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dl" > <LinearLayout android:layout_gravity="left" android:layout_width="300dp" android:orientation="vertical" android:background="#2d7bda" android:layout_height="match_parent"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:src="@color/colorAccent"/> <TextView android:id="@+id/tv0" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="韩海"/> <TextView android:id="@+id/tv01" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="月考练习"/> <TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="练习第一次"/> <TextView android:id="@+id/tv2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv1" android:text="练习第二次"/> <TextView android:id="@+id/tv3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv2" android:text="练习第三次"/> <TextView android:id="@+id/tv4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv3" android:text="练习第四次"/> <TextView android:id="@+id/tv5" android:layout_width="match_parent" android:layout_below="@id/tv4" android:layout_height="wrap_content" android:text="练习第五次"/> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:id="@+id/sy" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="首页" /> <Button android:id="@+id/qy" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="详情页" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/ll" android:id="@+id/fl"></FrameLayout> </RelativeLayout> </android.support.v4.widget.DrawerLayout> </RelativeLayout>