首页 > 解决方案 > 如何添加 NavigationView 抽屉 Android 的图标

问题描述

我是一名新的应用程序开发人员。我做了一个(NavigationView 抽屉)的例子,效果很好。但我的问题是我需要知道如何添加它。

像这样:

在此处输入图像描述

我已在活动 bar_main 中将其添加为 ImageButton,但我不需要那样。因此,我有以下问题:

在此处输入图像描述

我见过很多例子,图标不是以这种方式添加的,它不是 ImageButton。这就像来自没有任何 ImageButton 的系统。所以我需要这样做。

现在,如果我这样工作,它将与搜索图标重叠。按下搜索按钮时,抽屉图标应该消失。

我的代码:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

static ImageButton  menuRight;
    private DrawerLayout drawer;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_m);

    
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        View headerView = navigationView.getHeaderView(0);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.bringToFront();
        toolbar =findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        menuRight =(ImageButton) findViewById(R.id.leftRight);
        menuRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else {
                    drawer.openDrawer(GravityCompat.START);
                }
            }
        });
        
    }
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        drawer.closeDrawers();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        int id = item.getItemId();
        if (id == R.id.nav_home) {

            Intent i = new Intent(this, MainActivity.class);
            startActivity(i);


        } else if (id == R.id.nav_Politics) {

            Intent i = new Intent(this, Ploysity.class);
            startActivity(i);

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;

        }

        return false;
    }
  

    public void onBackPressed() {

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            }
            
        super.onBackPressed();
    }

  
}


<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"></androidx.appcompat.widget.Toolbar>

        <ImageButton
            android:id="@+id/leftRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimary"
            android:src="@drawable/ic_menu_black_24dp" />

    </RelativeLayout>

</com.google.android.material.appbar.AppBarLayout>



标签: android

解决方案


如果你想改变图标,那么你需要添加这一行:

drawer.setHomeAsUpIndicator(R.drawable.ic_your_drawer_icon); //set your own


推荐阅读