android的DrawerLayout使用

来源:互联网 发布:淘宝的安逸猿 编辑:程序博客网 时间:2024/06/06 03:00

布局


侧滑菜单的frameLayout放在末尾,不然会抢点击事件

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout    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:id="@+id/drawerLayout"    tools:context="com.hlh.multimedia.MainActivity">    <FrameLayout        android:id="@+id/main_interface"        android:layout_width="match_parent"        android:layout_height="match_parent"/>    <FrameLayout        android:id="@+id/left_menu"        android:layout_width="200dp"        android:layout_height="match_parent"        android:layout_gravity="left"        android:background="#f00008"/></android.support.v4.widget.DrawerLayout>

代码

.//大概就是这样

public class MainActivity extends FragmentActivity {    private FrameLayout mLeftMenu;    private DrawerLayout mDrawerLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mLeftMenu = (FrameLayout) findViewById(R.id.left_menu);        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);        View left = new TextView(this);        mLeftMenu.addView(left);//把布局填充到侧滑菜单里面 }  }一些方法
if (mDrawerLayout.isDrawerVisible(mLeftMenu))//判断抽屉布局是否可见    mDrawerLayout.openDrawer(mLeftMenu);        //打开抽屉else     mDrawerLayout.closeDrawer(mLeftMenu);      //关闭抽屉