pre-lollipop 台设备上的奇怪自定义工具栏
Strange custom Toolbar on pre-lollipop devices
我在低于 5.0 的设备上遇到了 appcompat 工具栏的问题。
我期待这个(在 Xperia 和 Nexus 设备上使用棒棒糖的工作结果):
不幸的是,我在 < 5.0 的设备上得到了这个;黑色文本和一个奇怪的状态栏悬停在工具栏上:
这是我的工具栏设计:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp">
</android.support.v7.widget.Toolbar>
和 MainActivity 设计:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar">
</include>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
我的风格-v21.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/ColorPrimary</item>
<item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
在 MainActivity 的 onCreate 中定义代码:
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
mRecyclerView.setHasFixedSize(true);
Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
Drawer.setStatusBarBackgroundColor(getResources().getColor(R.color.ColorPrimaryDark));
我不确定这个错误的原因。但我认为,如果您在 Lollipop 中设置 Statusbar 背景色,可以解决最近引入的 API
API 是 setStatusBarColor(int color)
,您需要为 WindowManager
设置一些有意义的标志:
从 this descriptive answer 中找到的样本:
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.ColorPrimary));
// to set ColorPrimary as status bar background color
这将删除棒棒糖的三星设备中显示的灰色背景
好吧,我终于设法解决了这个奇怪的问题。我已经将 v-19 的 style.xml 更改为:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="android:windowTranslucentStatus">true</item>
</style>
这只是让 kitkat 设备上的状态栏透明。并没有改变样式-v21.xml 和原始 styles.xml 文件。
然后更改工具栏添加app:theme
和app:popupTheme
设置为显示白色文本主题。还有一个 android:paddingTop
来更改需要在 kitkat 设备上设置的填充。
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimary"
android:elevation="4dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:paddingTop="@dimen/tool_bar_top_padding">
</android.support.v7.widget.Toolbar>
在默认的dimens.xml:
<dimen name="tool_bar_top_padding">0dp</dimen>
维度-v19.xml:
<dimen name="tool_bar_top_padding">20dp</dimen>
维度-v21.xml:
<dimen name="tool_bar_top_padding">0dp</dimen>
最后但同样重要的是,从 main_activity.xml 设计
的 DrawerLayout 中删除了 android:fitsSystemWindows="true"
我在低于 5.0 的设备上遇到了 appcompat 工具栏的问题。 我期待这个(在 Xperia 和 Nexus 设备上使用棒棒糖的工作结果):
不幸的是,我在 < 5.0 的设备上得到了这个;黑色文本和一个奇怪的状态栏悬停在工具栏上:
这是我的工具栏设计:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp">
</android.support.v7.widget.Toolbar>
和 MainActivity 设计:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar">
</include>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
我的风格-v21.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/ColorPrimary</item>
<item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
在 MainActivity 的 onCreate 中定义代码:
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
mRecyclerView.setHasFixedSize(true);
Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);
Drawer.setStatusBarBackgroundColor(getResources().getColor(R.color.ColorPrimaryDark));
我不确定这个错误的原因。但我认为,如果您在 Lollipop 中设置 Statusbar 背景色,可以解决最近引入的 API
API 是 setStatusBarColor(int color)
,您需要为 WindowManager
设置一些有意义的标志:
从 this descriptive answer 中找到的样本:
Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.ColorPrimary));
// to set ColorPrimary as status bar background color
这将删除棒棒糖的三星设备中显示的灰色背景
好吧,我终于设法解决了这个奇怪的问题。我已经将 v-19 的 style.xml 更改为:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="android:windowTranslucentStatus">true</item>
</style>
这只是让 kitkat 设备上的状态栏透明。并没有改变样式-v21.xml 和原始 styles.xml 文件。
然后更改工具栏添加app:theme
和app:popupTheme
设置为显示白色文本主题。还有一个 android:paddingTop
来更改需要在 kitkat 设备上设置的填充。
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/ColorPrimary"
android:elevation="4dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:paddingTop="@dimen/tool_bar_top_padding">
</android.support.v7.widget.Toolbar>
在默认的dimens.xml:
<dimen name="tool_bar_top_padding">0dp</dimen>
维度-v19.xml:
<dimen name="tool_bar_top_padding">20dp</dimen>
维度-v21.xml:
<dimen name="tool_bar_top_padding">0dp</dimen>
最后但同样重要的是,从 main_activity.xml 设计
的 DrawerLayout 中删除了android:fitsSystemWindows="true"