DrawLayout和SlidingMenu

DrawLayout布局

<LinearLayout 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.day3.MainActivity"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:background="#bdbebf"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp">
    </android.support.v7.widget.Toolbar>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--主界面,必须先写-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            >
            <TextView
                android:text="我是侧拉主界面"
                android:textSize="50sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <!--侧滑部分  android:layout_gravity="left" 从左侧滑出-->
        <LinearLayout
            android:layout_gravity="left"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:gravity="center">
            <ListView
                android:id="@+id/lv"
                android:background="#b49797"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"></ListView>
        </LinearLayout>

    </android.support.v4.widget.DrawerLayout>


</LinearLayout>

DrawLayoutMainActivity.java

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        setContentView(R.layout.activity_main);
        drawerLayout=findViewById(R.id.drawer_layout);
        initToolBar();//初始化bar
        bindToolBar();//绑定bar
    }
    private void initToolBar() {

        toolbar = findViewById(R.id.toolbar);
        //设置导航图标.
        toolbar.setNavigationIcon(R.mipmap.ic_launcher);
        //设置toolbar的大标题
        toolbar.setTitle("天气");
        toolbar.setLogo(R.mipmap.ic_launcher_round);
        //设置toolbar的小标题.
        toolbar.setSubtitle("Android");
      

    }
   
}

SlidingMenuActiyity

public class SlidingMenuActivity extends AppCompatActivity {
    private SlidingMenu slidingMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sliding_menu);
        initmenu();
    }

    private void initmenu() {
        //TODO 1:创建菜单对象
        slidingMenu=new SlidingMenu(this);
        //TODO 2:设置属性
        //SlidingMenu.RIGHT   SlidingMenu.LEFT
        slidingMenu.setMode(SlidingMenu.LEFT);//设置从哪边出来
        //TOUCHMODE_FULLSCREEN全屏;TOUCHMODE_MARGIN边界;TOUCHMODE_NONE不能滑动
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//在屏幕的任何位置左滑都可以显示出来
        slidingMenu.setBehindOffset(200);//单位是像素px 侧滑菜单滑出后,界面剩余的宽度。
        slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//绑定到指定的Context
        //TODO 3:设置布局并且相应布局上面的按钮
      View view = LayoutInflater.from(this).inflate(R.layout.slide, null);
Button button=view.findViewById(R.id.bt_close);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //关闭抽屉
       // slidingMenu.showMenu();//显示抽屉
        slidingMenu.showContent();//显示主页面
    }
});
slidingMenu.setMenu(view);//设置布局        //TODO 4:设置监听listener,监听侧滑菜单的打开和关闭.
        slidingMenu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
            @Override
            public void onOpened() {
                Toast.makeText(SlidingMenuActivity.this, "打开了", Toast.LENGTH_SHORT).show();
            }
        });
        slidingMenu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
            @Override
            public void onClosed() {
                Toast.makeText(SlidingMenuActivity.this, "侧滑菜单关闭", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

你可能感兴趣的:(DrawLayout和SlidingMenu)