工具栏在 android 4.4 平板电脑中不可见
Toolbar is not visible in android 4.4 tablet
我有以下布局我正在添加一个工具栏。 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 的情况下解决此问题。
感谢您的帮助。
您尝试在 android 清单中为您的活动声明 android 主题。
例如:
<activity android:name=".activities.MainActivity"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:screenOrientation="portrait">
</activity>
在您的工具栏样式中添加这些属性。
<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>
你的 AppBarLayout
被你的 RelativeLayout
遮挡了。
来自 CoordinatorLayout
文档:
CoordinatorLayout is a super-powered FrameLayout.
FrameLayout
将按定义顺序绘制子项。所以你的 AppBarLayout
先画,然后你的 RelativeLayout
画。因为您的 RelativeLayout
的宽度和高度为 match_parent
,所以它会填满整个屏幕。
所以问题是为什么这只发生在某些 API 级别上,因为我的描述似乎适用于任何 API 级别。
原来 CoordinatorLayout
比 FrameLayout
聪明一点,在较新的 API 关卡上它可以做一些特殊的事情来确保你的 AppBarLayout
可见的。在较旧的 API 级别上,默认情况下无法执行此操作。
但是,您可以通过向 <RelativeLayout>
标签添加 app:layout_behavior
属性来解决此问题。只需更改此:
<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">
此属性将触发智能行为以确保您的 AppBarLayout
始终可见,即使在较旧的 API 级别上也是如此。
我有以下布局我正在添加一个工具栏。 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 的情况下解决此问题。
感谢您的帮助。
您尝试在 android 清单中为您的活动声明 android 主题。
例如:
<activity android:name=".activities.MainActivity"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:screenOrientation="portrait">
</activity>
在您的工具栏样式中添加这些属性。
<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>
你的 AppBarLayout
被你的 RelativeLayout
遮挡了。
来自 CoordinatorLayout
文档:
CoordinatorLayout is a super-powered FrameLayout.
FrameLayout
将按定义顺序绘制子项。所以你的 AppBarLayout
先画,然后你的 RelativeLayout
画。因为您的 RelativeLayout
的宽度和高度为 match_parent
,所以它会填满整个屏幕。
所以问题是为什么这只发生在某些 API 级别上,因为我的描述似乎适用于任何 API 级别。
原来 CoordinatorLayout
比 FrameLayout
聪明一点,在较新的 API 关卡上它可以做一些特殊的事情来确保你的 AppBarLayout
可见的。在较旧的 API 级别上,默认情况下无法执行此操作。
但是,您可以通过向 <RelativeLayout>
标签添加 app:layout_behavior
属性来解决此问题。只需更改此:
<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">
此属性将触发智能行为以确保您的 AppBarLayout
始终可见,即使在较旧的 API 级别上也是如此。