2015-04-20 59 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

相關問題