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



因此,我已经在我的应用程序中实现了可折叠的工具栏,在Java类中,我覆盖了ofride increateoptionsmenu和onoptionsItemectemectement of the:

 @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);
    }

现在,当我打开活动时,我可以看到菜单图标并与它们一起做事,直到工具栏被填充为止。当用户colaps工具栏时,图标消失了。

第二个问题,使用getSupportActionBar().setDisplayHomeAsUpEnabled(true);方法我获得了黑色的返回按钮,而不是白色,所以我现在使用此方法:

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

我对此问题的问题是如何处理此导航图标的点击事件?当工具栏搭配并离开左填充时,该图标也会消失,但不是。

这是我的代码:

<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">

我在做什么错?

您是否正在使用旧版本的支持库(23.0.1)?如果是,请查看图标是否随着最新版本而消失。

要使用白色后背按钮,将其添加到style.xml

<style name="MyToolbarLight" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorControlNormal">@color/white</item>
</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"
/>

您的图标正在消失,因为您没有为工具栏设置倒塌模式,因此在开始滚动时不会保持修复,请将其添加到工具栏中:

 app:layout_collapseMode="pin"

注意:如果您包括工具栏布局,则必须指定(或重复)宽度和高度值,collapsemode还不够:

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

最新更新