【问题标题】:CollapsingToolbarLayout hides my back buttonCollapsingToolbarLayout 隐藏了我的后退按钮
【发布时间】:2020-05-26 09:56:23
【问题描述】:

我创建了折叠工具栏,但似乎无法使后退按钮或其他菜单项膨胀。

我像这样膨胀我的后退按钮:

    toolbar_detail = findViewById(R.id.tool_de);
    setSupportActionBar(toolbar_detail);

    if (toolbar_detail != null) {
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);}

这是我的折叠应用栏布局:

  <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    tools:context=".Detail">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:id="@+id/app_bar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/coolaps_tool_bar"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@color/colorPrimary">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:src="@drawable/ic_launcher_background"
                android:contentDescription="zx" />

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/tool_de"
                android:layout_width="0dp"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:elevation="4dp"
                app:layout_collapseMode="pin"
               app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:titleTextColor="@android:color/white" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>


    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="sdsaa"
            />
    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

我该如何解决这个问题? 我尝试了几种方法,例如指定父活动等似乎不起作用

【问题讨论】:

  • 如果可能的话,添加整个 XML 文件,以便我们可以在 Android Studio 中对其进行测试。

标签: android android-layout android-coordinatorlayout


【解决方案1】:
  1. 0dp 在你的 &lt;Toolbar&gt; 中没有意义,所以让它成为 match_parent
  2. 通过以下方式之一添加您的图标:

直接在工具栏上添加返回按钮:

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:navigationIcon="@drawable/my_back_button_icon"/>

如果您想使用默认的ActionBar 后退按钮:

getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

请务必在您的 AndroidManifest 中设置父级:

<activity android:name=".SecondActivity" android:parentActivityName=".MainActivity">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 2014-11-17
    • 2012-04-21
    • 2016-06-19
    • 2015-12-01
    相关资源
    最近更新 更多