Android 工具栏在 XML 中没有layout_below时不显示



我有一个关于三星Galaxy Tab 4中的Android工具栏的奇怪问题。也许是操作系统(4.4.2)或设备本身。我正在使用 AppCompat-v7:23 库来显示棒棒糖前设备的工具栏。

以下是显示工具栏的活动的 XML 代码:

<LinearLayout 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"
android:fitsSystemWindows="true">
<include
    layout="@layout/appbar_content_top"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

这是appbar_content_top:

<FrameLayout 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=".CEMainActivity">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_alignParentTop="true"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

如您所见,我没有为 @+id/content 放置 android:layout_below 属性。这是我的要求之一。在我的活动中,我获取工具栏并将其设置为我的支持操作工具栏。

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
    Log.e("Viewer", "Toolbar is not null");
} else {
    Log.e("Viewer", "Toolbar is null");
}

工具栏显示其他设备。但是在Galaxy Tab 4中,工具栏没有显示,即使上面的代码片段说工具栏不为空。

奇怪的是,如果我在@ + id/content中添加android:layout_below属性,工具栏将显示在Galaxy Tab 4中。

关于为什么会发生这种情况的任何线索?谢谢!

由于您使用的是 appbarlayout,请使用协调器布局而不是 Framelayout 作为appbar_content_top.xml的主视图组,协调器的行为方式与框架布局相同,只需将其视为超级框架布局;)还有一件事layout_below仅将相对布局作为父视图组正常工作

最新更新