简体   繁体   English

Android可折叠工具栏菜单图标消失

[英]Android collapsable Toolbar menu icons disappearing

So I've implemented collapsable Toolbar inside my app and in Java class I override onCreateOptionsMenu and onOptionsItemSelected like this: 所以我在我的应用程序中实现了可折叠工具栏,在Java类中,我重写onCreateOptionsMenu和onOptionsItemSelected,如下所示:

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_share:
                break;
            case R.id.action_addToFavorites:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

And now when I open the activity I can see the menu icons and do stuff with them until the toolbar is colapsed. 现在,当我打开活动时,我可以看到菜单图标,并使用它们做任务,直到工具栏被折叠。 When user colapses the toolbar the icons disappears. 当用户瘫痪工具栏时,图标会消失。

Second problem, with using the getSupportActionBar().setDisplayHomeAsUpEnabled(true); 第二个问题,使用getSupportActionBar().setDisplayHomeAsUpEnabled(true); method i get back button of black color instead of white so now I'm using this method: 方法我得到黑色而不是白色的按钮,所以现在我使用这种方法:

tToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_navigation_arrow_back));

My question for this problem is how to handle click events for this navigation icon? 我对此问题的疑问是如何处理此导航图标的点击事件? This icon also disappears when toolbar colapses and leaves the left padding like it's there, but it's not. 当工具栏瘫痪时,此图标也会消失,并且左边的填充就像它在那里一样,但事实并非如此。

Here's my code: 这是我的代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.darioradecic.topmusicandartists.Details">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/MyAppbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <com.android.volley.toolbox.NetworkImageView
                android:id="@+id/imageViewDetailsTopGlobalSongs"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="pin" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/tToolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"

                />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:padding="10dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

What am I doing wrong? 我究竟做错了什么?

Are you using old version of Support Library(23.0.1) ? 您使用旧版本的支持库(23.0.1)吗? If yes then see if the icons doesn't disappear with the latest version . 如果是,则查看图标是否随最新版本消失。

To use white color back button , add this to style.xml 要使用白色后退按钮,请将其添加到style.xml

<style name="MyToolbarLight" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorControlNormal">@color/white</item>
</style>

Then in your layout xml , add the style . 然后在布局xml中添加样式。

<android.support.v7.widget.Toolbar
android:id="@+id/tToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/MyToolbarLight"
/>

Your icon is disappearing because you haven't set collapse mode for the toolbar so it doesn't stay fixed when you start scrolling, simpy add this to your toolbar : 您的图标正在消失,因为您没有为工具栏设置折叠模式,因此当您开始滚动时它不会保持固定,只需将其添加到您的工具栏:

 app:layout_collapseMode="pin"

Note: if you're including your toolbar layout you must specify (or repeat) the width and height values, collapseMode will not be enough: 注意:如果您要包含工具栏布局,则必须指定(或重复)宽度和高度值,collapseMode将不够:

<include layout="@layout/view_toolbar"
         android:layout_width="match_parent"
         android:layout_height="?attr/actionBarSize"
         app:layout_collapseMode="pin"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM