Android UI 之侧滑抽屉菜单(一)——DrawerLayout + NavigationView

本文出自:http://blog.csdn.net/dt235201314/article/details/78962509

一丶概述

Android侧滑效果实现有很多种,这里先说说,Android系统5.0后自带的DrawerLayout + NavigationView。

二丶效果图

1.gif

三丶看代码

DrawerLayout + NavigationView xml布局



    

        

            
            
            

        
        
    

    


NavigationView里的headerLayout



    
    

    


menu




    
        
        
    


分别对应

属性设置注意点:

1.android:layout_gravity="left"或者“star”表示从左边弹出,这里跟后面java代码关闭相关

drawerLayout.closeDrawer(GravityCompat.START);

2.app:itemIconTint="@color/blue" 设置菜单选项图片颜色,不写默认为无色(灰色),如需要现实为图片原本颜色java代码设置

navigationView.setItemIconTintList(null);

Activity java代码

public class MyCsdnActivity extends Activity implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout drawerLayout;
    private CircleImageView circleImageView;

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

    private void initView() {
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        circleImageView = (CircleImageView) findViewById(R.id.iv_mine_avatar);
        NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setItemIconTintList(null);
        circleImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(GravityCompat.START);
            }
        });
    }

    /**
     * 菜单点击响应函数
     *
     * @param item 菜单
     * @return boolean
     */
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        drawerLayout.closeDrawer(GravityCompat.START);
        switch (item.getItemId()) {
            case R.id.nav_csdn:
                WebViewActivity.startWebViewActivity(this,"http://my.csdn.net/dt235201314");
                break;
            case R.id.nav_jianshu:
                WebViewActivity.startWebViewActivity(this,"http://www.jianshu.com/u/905c7de5ae83");
                break;
            default:
        }
        return false;
    }

    /**
     * 重写返回键响应函数
     */
    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

3.NavigationView头部布局的id

NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
View headerView = navigationView.getHeaderView(0);
tv_name = (TextView) headerView.findViewById(R.id.tv_name);
tv_originalArticleNumber = (TextView) headerView.findViewById(R.id.tv_originalArticleNumber);
tv_visitNumber = (TextView) headerView.findViewById(R.id.tv_visitNumber);

四丶文章参考

Android5.0之NavigationView的使用

五丶代码下载欢迎关注点赞

https://github.com/JinBoy23520/CoderToDeveloperByTCLer

如果文章对你有帮助欢迎专注Star

你可能感兴趣的:(Android UI 之侧滑抽屉菜单(一)——DrawerLayout + NavigationView)