首页 > 解决方案 > 在导航抽屉中,三个破折号或抽屉图标未显示

问题描述

活动主

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    tools:context=".MainActivity">

    <com.google.android.material.navigation.NavigationView
        app:headerLayout="@layout/drawer_header"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        app:itemTextColor="#2d2d2d"
        app:itemIconTint="#AAD825"
        app:menu="@menu/drawer_menu"
        android:layout_gravity="start"/>

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerView" />

</androidx.drawerlayout.widget.DrawerLayout>

爪哇

private DrawerLayout mdrawerLayout;
    private ActionBarDrawerToggle mToggle;

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


        mdrawerLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
        mToggle= new ActionBarDrawerToggle(this,mdrawerLayout,R.string.open,R.string.close);
        mdrawerLayout.addDrawerListener(mToggle);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        mToggle.syncState();



        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this,3));
        loadProducts();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (mToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

依赖

  implementation 'com.android.support:design:29.0.2'
    implementation 'com.android.support:support-v4:29.0.2'

菜单(抽屉菜单)

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="Home Page" android:id="@+id/home_page" android:icon="@drawable/home_icon"></item>
    <item android:title="Search Car" android:id="@+id/search_car" android:icon="@drawable/search_icon"></item>
    <item android:title="My Profile" android:id="@+id/my_profile" android:icon="@drawable/profile"></item>
    <item android:title="Update Profile" android:id="@+id/update_profile" android:icon="@drawable/profile_update_icon"></item>
    <item android:title="Log Out" android:id="@+id/logout" android:icon="@drawable/logout_icon"></item>


</menu>

抽屉头.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C5E075">

    <ImageView
            android:layout_gravity="center"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:id="@+id/draw_photo"
            android:src="@mipmap/ic_launcher" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Name"
        android:textStyle="bold"
        android:id="@+id/draw_text"
        android:layout_gravity="center"
        android:textSize="25dp"
        android:textColor="#2d2d2d"/>


</LinearLayout>

细绳

<resources>
    <string name="app_name">Bs</string>
    <string name="open">Open</string>
    <string name="close">Close</string>
</resources>

标签: androidnavigation-drawer

解决方案


推荐阅读