奥利奥设备上的工具栏变空,但相同的代码适用于牛轧糖和更低版本

Moh*_*ani 5 android android-fragments android-toolbar android-8.0-oreo

我在 Oreo OS 上遇到了现有应用程序的问题。对于 Oreo 上的相同代码,工具栏变为空,而它适用于牛轧糖和更低版本。

下图显示了应用程序启动时的状态。我们可以看到工具栏是可见的并且具有所需的文本。
棉花糖首次推出
奥利奥首次推出

而当我们尝试通过单击按钮或导航抽屉来更改片段时,我们看到工具栏数据为空并且汉堡图标丢失。
Marshmallow Fragment Change
Oreo Fragment Change

我确信这段代码不是编写代码的好方法,并且使用了很多不正确的代码或工作问题。但是这段代码已经通过了多个团队的手,因此质量很差。我们正在努力改进代码。


请找到下面所附的代码以供参考。

工具栏布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentInsetStart="0dp"
    app:layout_scrollFlags="scroll|enterAlways"
    tools:ignore="MissingPrefix">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

            <LinearLayout
                android:id="@+id/toolBar_leftIconsLinearLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="9dp"
                android:layout_marginRight="7dp"
                android:orientation="horizontal">

            </LinearLayout>

            <TextView
                android:id="@+id/toolBar_titleTextView"
                fontPath="fonts/Roboto-Medium.ttf"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:layout_toEndOf="@+id/toolBar_leftIconsLinearLayout"
                android:layout_toRightOf="@+id/toolBar_leftIconsLinearLayout"
                android:text="@string/app_name"
                android:textColor="@color/black"
                android:textSize="20sp"
                android:visibility="gone" />

            <LinearLayout
                android:id="@+id/toolBar_rightIconsLinearLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="7dp"
                android:orientation="horizontal">

            </LinearLayout>

        </RelativeLayout>

    </FrameLayout>

</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

基础活动:

public void setupActionBar(boolean backEnabled, String title) {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        if (backEnabled) {
            addLeftIcon(R.drawable.back, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onHomePressed();
                }
            });
        }

        if (title != null && !title.isEmpty()) {
            changeToolbarText(title);
        }

        setSupportActionBar(toolbar);
    }
}
Run Code Online (Sandbox Code Playgroud)

主活动没有任何操作栏或工具栏代码。这些片段包括它的布局。

中的onActivityCreated方法Fragment具有以下用于设置操作栏的代码。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ((BaseActivity) getActivity()).setupActionBar(false, getString(R.string.myOrdersFragment_toolbarTitle));

    ((BaseActivity) getActivity()).addLeftIcon(R.drawable.menu, mLeftToolbarIconListener);

    ((MainActivity) getActivity()).enableSwipeToRefresh(true);
}
Run Code Online (Sandbox Code Playgroud)

请让我知道我在这里缺少什么。我的假设是,Android 可能已经使用 Fragment 升级了一些可能不支持 Toolbar 更改或 Support Action Bar 更改的东西;setSupportActionBar(toolbar);如果操作栏已经设置,则可能无法工作。