旧版Android设备上的奇怪自定义工具栏

3
我在5.0以下的设备上使用appcompat工具栏遇到了问题。我期望的结果是在Xperia和Nexus设备上工作的(如下图所示): Expecting result 但是在5.0以下的设备上,我得到的结果是黑色文本和一个奇怪的状态栏悬浮在工具栏上(如下图所示): AppBar on a 4.4 device 这是我的工具栏设计:
<?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>

My styles-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));
2个回答

1

好的,我终于成功解决了这个奇怪的问题。我已经将v-19的style.xml更改为以下内容:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" >
    <item name="android:windowTranslucentStatus">true</item>
</style>

这只是在KitKat设备上使状态栏透明。并没有更改style-v21.xml和原始styles.xml文件的任何内容。
然后将工具栏更改为添加app:themeapp: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>

dimens-v19.xml:

<dimen name="tool_bar_top_padding">20dp</dimen>

dimens-v21.xml:

<dimen name="tool_bar_top_padding">0dp</dimen>

最后但同样重要的是,在main_activity.xml设计中从DrawerLayout中删除android:fitsSystemWindows="true"


0

我不确定这个错误的原因。但是我认为,如果你最近引入的API中设置了Lollipop状态栏背景颜色,那么可以做一个解决方法。

该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

这将移除在棒棒糖版本的三星设备上显示的灰色背景


状态栏背景不会随上述代码更改吗? - Kushal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接