簡體   English   中英

Android可折疊工具欄菜單圖標消失

[英]Android collapsable Toolbar menu icons disappearing

所以我在我的應用程序中實現了可折疊工具欄,在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);
    }

現在,當我打開活動時,我可以看到菜單圖標,並使用它們做任務,直到工具欄被折疊。 當用戶癱瘓工具欄時,圖標會消失。

第二個問題,使用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"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM