2016-07-09 72 views
1

我將通過styles.xml更改狀態欄顏色,但無法設置狀態欄顏色。我不想通過編碼設置狀態欄的顏色,我只想通過theme/style通過主題設置StatusBar顏色。

Activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="eazitouk.eazitoclient.mainclasses.TestActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_test" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

的Manifest.xml

<application 
     android:name="eazitouk.eazitoclient.miscs.AppController" 
     android:icon="@drawable/ic_launchicon" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".TestActivity" 
      android:label="@string/title_activity_test" 
      android:theme="@style/AppTheme1"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

color.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <color name="ColorPrimary">#df4a43</color> 
    <color name="ColorPrimaryDark">#c8423c</color> 

    <color name="ColorPrimary1">#FFCF3B</color> 
    <color name="ColorPrimaryLight">#F3A2A0</color> 
    <color name="ColorPrimaryLightShade">#e56e66</color> 

</resources> 

Style.xml

<resources> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light"> 
     <item name="colorPrimary">@color/ColorPrimary</item> 
     <item name="colorPrimaryDark">@color/ColorPrimaryDark</item> 
    </style> 

    <style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimary">@color/ColorPrimary</item> 
     <item name="colorPrimaryDark">@color/ColorPrimaryDark</item> 
     <item name="android:textColorSecondary">@color/accentcolor</item>--> 
    </style> 
</resources> 

這是我style.xml和AppTheme是適用於應用和AppTheme1適用於我的活動,但不能設置狀態欄顏色

+0

您是否在colors.xml中設置了顏色? –

+0

是的,我設定了。但不能工作。 –

+0

@akhil batlawala你在測試哪個設備? – Ironman

回答

3

我已經測試你的代碼的地方使用顏色代碼。現在改成你的style有問題。所以按照我的方式來獲得輸出。

Android manifest.xml。

<application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:largeHeap="true" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme"> 

     <activity 
      android:name=".TestActivity" 
      android:label="@string/title_activity_test" 
      android:theme="@style/AppTheme"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

colors.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <color name="primaryColor">#EF6C00</color> 
    <color name="primaryColorDark">#E65100</color> 
    <color name="accentcolor">#0072BA</color> 

</resources> 

styles.xml

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/primaryColor</item> 
     <item name="colorPrimaryDark">@color/primaryColorDark</item> 
     <item name="colorAccent">@color/accentcolor</item> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 


</resources> 

風格。XML(V21)

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="android:statusBarColor">@android:color/transparent</item> 
    </style> 

</resources> 

最終輸出:

enter image description here

,我還有一點請您已設置targetSdkVersion 23頭腦。

+0

@akhil batlawala嘿'CGPIT'你解決了嗎? – Ironman

+0

CGPIT的意思是...? –

+0

@akhilbatlawala是否嘗試「清理」該項目? – Ironman

0

ColorPrimaryDark是對應的狀態欄的顏色之一。

https://developer.android.com/training/material/theme.html

所以:

<item name="colorPrimaryDark">@color/ColorPrimaryDark</item> 

添加與名稱colors.xml您想要的顏色ColorPrimaryDark

+0

雅我知道但不能工作。請給我另一個解決方案。 –

+0

您在colors.xml中沒有您的顏色,或者您的AppTheme或AppTheme1都未被選中。 –

+0

是的,我設定了。但不能工作。 –

1

只是試圖給出六色代碼的顏色。看起來color.xml文件中的顏色ColorPrimaryDark不包含您想要的顏色。

嘗試:

<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar"> 
    ... 
    <item name="colorPrimaryDark">#fad</item> 
    ... 
</style> 

上的#fad

+0

請結帳我編輯我的問題 –

+0

這不起作用。 –

+0

您必須將style.xml文件中的顏色代碼直接提供給item標籤。 – lRadha