预棒棒糖设备上的奇怪自定义工具栏

时间:2015-04-20 11:55:07

标签: android android-5.0-lollipop android-toolbar

我在<的appcompat工具栏上遇到了问题。 5.0设备。 我期待这个(使用棒棒糖的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>

我的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中定义代码&on;创建:

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 个答案:

答案 0 :(得分: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>

梦诗-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"

答案 1 :(得分: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

这将删除棒棒糖三星设备中显示的灰色背景

相关问题