工具栏未显示个人资料图标

时间:2018-09-20 14:42:53

标签: android android-toolbar

我的HomeActivity中有一个工具栏。我想在工具栏中添加一个配置文件图标。我已经在包含该项目的菜单文件夹中创建了一个菜单文件,并且还在活动的onCreate中添加了getSupportActionBar。我不确定为什么它仍不显示图标。

MainActivity:

public class HomeScreen extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout mDrawer;
    private NavigationView nvDrawer;
    private ActionBarDrawerToggle drawerToggle;
    FragmentTransaction fragmentTransaction;
    MaterialSearchView searchView;



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

        setContentView(R.layout.activity_home);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        searchView = (MaterialSearchView) findViewById(R.id.search_view);

        // Find our drawer view
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        nvDrawer = (NavigationView) findViewById(R.id.nvView);
        drawerToggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawer.addDrawerListener(drawerToggle);
        setupDrawerContent(nvDrawer);
        setFirstItemNavigationView(nvDrawer);


    }

    private void setFirstItemNavigationView(NavigationView navigationView) {

        navigationView.setCheckedItem(R.id.nav_home);
        navigationView.getMenu().performIdentifierAction(R.id.nav_home, 0);
    }

    public void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                selectDrawerItem(menuItem);
                return true;
            }
        });

    }

    private void selectDrawerItem(MenuItem menuItem) {

        switch (menuItem.getItemId()) {
            case R.id.nav_home:
                fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.main_container, new NewDiscoveryScreen());
                fragmentTransaction.commit();
                menuItem.setChecked(true);
                mDrawer.closeDrawers();
                break;
            case R.id.nav_setting:
                fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.main_container, new SettingsFragment());
                fragmentTransaction.commit();
                menuItem.setChecked(true);
                mDrawer.closeDrawers();
                break;
            case R.id.nav_profile:
                fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.main_container, new DiscoveryScreen());
                fragmentTransaction.commit();
                menuItem.setChecked(true);
                mDrawer.closeDrawers();
                break;
            default:
                fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.main_container, new NewDiscoveryScreen());
                fragmentTransaction.commit();
                menuItem.setChecked(true);
                mDrawer.closeDrawers();
        }

    }




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

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

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

                break;
            default:
                break;
        }
        return true;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.onConfigurationChanged(newConfig);
    }
}

Style.xml:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorAccent</item>
        <item name="colorPrimaryDark">@color/colorAccent</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="cardViewStyle">@style/CardView</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="ToolBarStyle" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="actionMenuTextColor">@android:color/white</item>
    </style>

    <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorPrimaryDark</item>
        <item name="android:textColorPrimary">@color/color_txtBlack</item>
        <item name="android:background">@color/color_dark_grey</item>
    </style>

menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_profile"
        android:title=""
        android:icon="@drawable/profile"
        app:showAsAction="always" />
</menu>

有人可以告诉我我想念的东西吗

1 个答案:

答案 0 :(得分:0)

看看您的代码 R.menu.menu_main 。你写的名字正确吗?
menu.xml 还是 menu_main.xml 。我没有看到其他错误。

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
 //---------------This is your Code ------------------
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);