2015-04-20 61 views
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> 

我的风格-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)); 

回答

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> 

最后,但至少从Dra中删除了android:fitsSystemWindows="true" werLayout在main_activity中。xml设计

0

我不知道原因,此错误的。但我认为一个解决办法可以,如果你在棒棒堂设置状态栏的背景颜色从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 

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

+0

不幸的是,没有解决我的问题。 – MegaCookie

+0

状态栏背景不会随上面的代码改变吗? – Kushal

相关问题