当XML中没有layout_below时,Android工具栏无法显示

时间:2016-02-23 02:54:11

标签: android xml android-toolbar galaxy-tab appcompat-v7-r23

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

以下是我的Activity的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属性。这是我的要求之一。在我的Activity中,我获取工具栏并将其设置为supportActionToolbar。

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中,即使上面的代码段显示工具栏不为空,也不会显示工具栏。

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

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

1 个答案:

答案 0 :(得分:0)

由于您正在使用appbarlayout,使用协调器布局而不是Framelayout作为appbar_content_top.xml的主视图组,协调器的行为与framelayout相同,只需将其视为超级framelayout;)以及另外一件事layout_below正常工作relativelayout仅作为父视图组