簡體   English   中英

Android - 溢出菜單和后退按鈕未顯示在折疊工具欄中

[英]Android - Overflow Menu and Back Button not showing in Collapsing Toolbar

我正在嘗試從新的設計支持庫中實現功能,以創建一個視差滾動工具欄,它看起來類似於新的材料設計WhatsApp配置文件頁面。 但是,我無法在頂角顯示溢出菜單和后退按鈕。

我已嘗試使用以下方法顯示后退按鈕,但它們都不起作用。

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

並且覆蓋溢出菜單的onCreateOptionsMenu方法也不起作用。

有誰知道如何從設計支持庫中將這些工具欄圖標添加到CollapsingToolbar? 下面是我的活動布局xml。 謝謝!

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/primary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/headerbg"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
            app:layout_collapseMode="pin" />

    </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:layout_marginBottom="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

</android.support.v4.widget.NestedScrollView>

我有同樣的問題,現有的答案都沒有幫助我,令人驚訝的解決我的問題是問題描述。


AppCompat活動的解決方案

因此,使用后退按鈕工作折疊工具欄需要控制器onCreate方法中的那幾行:

//change id to Your id
toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar);

//this line shows back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

此活動的清單(請注意parentActivityName屬性):

<activity
  android:name=".SomeActivity"
  android:parentActivityName=".MainActivity"
  android:theme="@style/AppTheme.NoActionBar"/>

模板部分:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"
            />

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

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

替代解決方案(也是AppCompat活動)

Controller onCreate方法:

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

toolbar.setNavigationIcon(android.support.v7.appcompat.R.drawable.abc_ic_ab_back_material);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            onBackPressed();
        }
});

此方法使用第一個解決方案中顯示的相同清單和模板。

在我的情況下這是有效的! app:layout_collapseMode="pin"工具欄布局中的app:layout_collapseMode="pin"

盡量不要將它放在coordinatorLayout中:

android:fitsSystemWindows="true"

這個對我有用。 不知道為什么。 祝好運

以下啟用導航按鈕:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp, null));
toolbar.setNavigationOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //onBackPressed()
    }
});

要獲得溢出菜單行為,您必須使用showAsAction聲明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/action_one"
        android:title="action 1"
        android:orderInCategory="1"
        app:showAsAction="never"/>

    <item android:id="@+id/action_two"
        android:title="action 2"
        android:orderInCategory="2"
        app:showAsAction="never"/>
</menu>

並膨脹它像:

toolbar.inflateMenu(R.menu.main);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_one:
                break;
            case R.id.action_two:
                break;
        }
        return true;
    }
});

在appView中將app:layout_collapseMode =“parallax”更改為app:layout_collapseMode =“pin”。 希望這對你有用

如果您尚未在清單文件中添加parentActivityName標記,請通過添加以下代碼行來執行此操作

<activity android:name=".Activity.MovieData"
        android:parentActivityName=".Activity.Home">
</activity>

其中“活動”是我的包名“MovieData”是我的活動名稱“Home”是我的目標活動名稱

編輯:如果您支持Android api等級15及以下,那么parentActivityName將無法正常工作。

要支持較低版本,您需要添加元數據標記

<meta-data
    android:name="PARENT_ACTIVITY"
    android:value="com.example.Activity.Home"/>

轉到res / values /並打開styles.xml

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

將parent =“Theme.AppCompat.Light.NoActionBar”更改為parent =“Theme.AppCompat.Light”

然后開始調試並刪除所有不必要的代碼,如// setSupportActionBar(toolbar); 的東西。 絕對有效。

改變這個..

        <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:elevation="15dp"

        android:layout_width="match_parent"
        app:logo="@drawable/action_bar_logo"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

popupTheme有所不同,這正是你要找的......

這是我的完整xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:fitsSystemWindows="true"
    tools:context=".AddUser">
        <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:elevation="15dp"
            android:layout_width="match_parent"
            app:logo="@drawable/action_bar_logo"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
            </android.support.design.widget.AppBarLayout>
        <include layout="@layout/content_add_user" />
</android.support.design.widget.CoordinatorLayout>

祝好運!

暫無
暫無

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

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