Android使用NavigationView和Toolbar和DrawLayout实现侧滑菜单

1、首先需要添加appcompat-v7支持:

如果是在Android Studio 2.1 Preview3 上创建的项目,默认已经添加了appcompat-v7和design支持了,如果不是最新版AndroidStudio则需要在build.gradle中添加如下代码:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.android.support:design:23.2.0'
    }

然后Ctrl+F9编译。

2、在主布局文件添加DrawerLayout:

activity_main.xml


    
    

    
    
    

3、添加包含ToolBar:

    在AndroidManifest.xml需要去掉自带的ActionBar:
    
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        
            
                

                
            
        
    
    定义toolbar.xml的样式



    

    

4、Java实现的核心代码:

package supermap.com.myapplication;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Main7Activity extends AppCompatActivity {

    @BindView(R.id.ly_content)
    FrameLayout lyContent;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;


    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.navigation_view)
    NavigationView navigationView;


    ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);
        ButterKnife.bind(this);


        // Logo
        toolbar.setLogo(R.mipmap.iv_icon_baidu);

        // 主标题
        toolbar.setTitle("Title");

        // 副标题
//        toolbar.setSubtitle("Sub Title");

        //设置toolbar
        setSupportActionBar(toolbar);

        //左上角图标可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        //左边的小箭头(注意需要在setSupportActionBar(toolbar)之后才有效果)
//        toolbar.setNavigationIcon(R.mipmap.iv_icon_douban);

        //菜单点击事件(注意需要在setSupportActionBar(toolbar)之后才有效果)
//        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                Log.e("b", false + "");
//                return true;
//            }
//        });

        mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };



        mDrawerToggle.syncState();
        //设置返回键动画
        drawerLayout.setDrawerListener(mDrawerToggle);



        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                BlankFragment contentFragment = new BlankFragment();
                Bundle args = new Bundle();
                args.putString("text", "text");
                contentFragment.setArguments(args);
                FragmentManager fm = getSupportFragmentManager();
                fm.beginTransaction().replace(R.id.ly_content, contentFragment).commit();
                drawerLayout.closeDrawer(navigationView);
                return true;
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_item, menu);
        return true;
    }

}


你可能感兴趣的:(Android)