Android 4.4平板电脑中看不到工具栏

时间:2017-09-12 19:52:48

标签: android android-layout android-coordinatorlayout

我在这里有以下布局我正在添加一个工具栏。在Android 6.0手机中一切都很好看。我刚刚在android 4.2平板电脑上尝试过它并且没有看到工具栏。这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.android.sushil.omdbclient.ui.main.MoviesList.MoviesListActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.AppBarLayout>

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

        <ProgressBar
            android:id="@+id/main_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:layout_gravity="center"/>

        <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/windowBackground"
            android:paddingTop="?attr/actionBarSize"
            android:visibility="visible">

        </android.support.v7.widget.RecyclerView>

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/search_to_begin"
            android:textSize="16sp"
            android:visibility="visible" />

        <!--<include layout="@layout/error_layout"/>-->
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

如果我使用Linearlayout更改协调器布局,我可以看到工具栏。但有人可以告诉我如何在不用Linerlayout替换协调器布局的情况下解决这个问题。

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您的AppBarLayout遮挡了RelativeLayout

来自CoordinatorLayout文档:

  

CoordinatorLayout是一个超级动力的FrameLayout。

FrameLayout将按照定义顺序绘制子项。因此,首先绘制AppBarLayout,然后绘制RelativeLayout。由于RelativeLayout的宽度和高度为match_parent,因此它会填满整个屏幕。

所以问题是为什么这只发生在某些API级别上,因为我的描述似乎应该适用于任何API级别。

事实证明CoordinatorLayoutFrameLayout稍微聪明一点,在较新的API级别上,它可以做一些特殊的事情来确保您的AppBarLayout可见。在较旧的API级别上,默认情况下无法执行此操作。

但是,您可以通过在app:layout_behavior标记中添加<RelativeLayout> attr来解决此问题。只需改变一下:

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

到此:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

此attr将触发智能行为,以确保您的AppBarLayout始终可见,即使在较旧的API级别也是如此。

答案 1 :(得分:0)

你冷试试在android清单中为你的活动声明android主题。

例如:

  <activity android:name=".activities.MainActivity"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:screenOrientation="portrait">
            </activity>

答案 2 :(得分:0)

在工具栏样式中添加这些属性。

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowActionBarOverlay">false</item>
    <item name="windowNoTitle">true</item>
</style>
</resources>