Android DrawerLayout抽屉效果和Toolbar标题栏的简单使用

话不多说先看运行效果:

Android DrawerLayout抽屉效果和Toolbar标题栏的简单使用_第1张图片

View层

布局layout:

activity_main.xml




    
    

app_bar_main.xml




    
        
            
        
    

    

content_main.xml





    

nav_header_main.xml





    

    

    

menu配置文件:

menu.xml



    
        
            
            
            
            
        
    
    

navigation_item.xml




    
        
        
        

        
        
    
    
        
            
            
        
    

values配置文件:

string.xml


    ActionBarUsing
    添加
    setting
    小明
    职位:极客
    Open navigation drawer
    Close navigation drawer
    标题
    创建群聊
    扫一扫
    加好友/群
    收付款

styles.xml



    
    
    
    
      
    

Control层

MainActivity.java

package com.example.actionbarusing;

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


public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }

    public void init() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("");//清空原标题
//        toolbar.setNavigationIcon(R.mipmap.ic_launcher_round);
        setSupportActionBar(toolbar);

        DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        toggle.setDrawerIndicatorEnabled(false);//取消左边三条横杠的图标
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();// 需要将ActionDrawerToggle与DrawerLayout的状态同步

        //setNavigationOnClickListener一定要放在setSupportActionBar(toolbar)
        // 和 drawerLayout.addDrawerListener(toggle)之后,不然onclick无效
        toolbar.setNavigationOnClickListener(this);//自定义图标打开左侧菜单

        NavigationView navigation = findViewById(R.id.nav_view);
        navigation.setNavigationItemSelectedListener(this);//给侧拉菜单添加监听事件

    }

    //Android中在按下back键时会调用到onBackPressed()方法,onBackPressed相对于finish方法
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        DrawerLayout layout = findViewById(R.id.drawer_layout);
        if (layout.isDrawerOpen(GravityCompat.START)) {
            layout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

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



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add:

                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

    //抽屉Item的选择方法
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.girl1:
                break;
            case R.id.girl2:
                break;
            case R.id.girl3:
                break;
            case R.id.girl4:
                break;
            case R.id.girl5:
                break;
            default:
                break;
        }
        DrawerLayout drawer=findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onClick(View v) {   //在定义图标打开左拉菜单点击事件
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.openDrawer(GravityCompat.START);
    }
}

 

你可能感兴趣的:(Android,Native,基础)