工具栏在Oreo设备上变空,但相同的代码适用于Nougat和更低版本

时间:2018-04-09 09:25:33

标签: android android-fragments android-toolbar android-8.0-oreo

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

以下图片显示应用程序启动时的状态。我们可以看到工具栏是可见的并且具有所需的文本。
Marshmallow First Launch
Oreo First Launch

当我们尝试通过某个按钮单击或导航抽屉更改片段时,我们看到工具栏数据为空并且汉堡包图标丢失。
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>

基础活动:

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

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

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

请让我知道我在这里失踪了什么。我的假设是,Android可能已经升级了一些可能不支持工具栏更改或支持操作栏更改的片段的东西;如果操作栏已经设置,setSupportActionBar(toolbar);可能无效。

0 个答案:

没有答案