2017-07-31 26 views
0

我想改變一個活動的ColorPrimaryDark的顏色。我怎樣才能做到這一點?如何才能將ColorPrimaryDark更改爲一項活動?

我覺得去到styles.xml和更改此:

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

但問題是,如果我這樣做,我改變我的所有活動的顏色,我只需要改變一個活動的顏色。

謝謝你的幫助!

具體而言,此顏色是應用程序頂部的酒吧顏色,我的意思是ActionBar以上。我使用Kotlin來做到這一點。在styles.xml文件中寫入主題

getWindow.setStatusBarColor(getResources().getColor(R.color.your_color)); 

此外,您還可以設置狀態欄的顏色:

<style name="YourActivityTheme" parent="AppTheme"> 
    <item name="colorPrimaryDark">@color/yourColor</item> 
</style> 

然後

+0

這可能會激勵你:https://developer.android.com/guide/topics/ui/themes.html#Inheritance – stkent

+0

你可以發佈你的styles.xml嗎? – UmarZaii

+0

您可以定義一種新的樣式,將其父定義爲應用中的常用樣式,並在其中重新定義colorPrimaryDark。針對此特定活動使用此新樣式 – HenriqueMS

回答

0

添加下面的代碼在你的活動以編程方式設置狀態欄的顏色在清單文件中,您必須添加以下代碼:

<activity android:name="packageName.YourActivity" 
    android:theme="@style/YourActivityTheme"/> 
3

Create a theme專門爲那個Activity

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimaryDark">@color/gray</item> 
    <!-- Your application theme --> 
</style> 

<style name="BlueActivityTheme" parent="AppTheme"> 
    <item name="colorPrimaryDark">@color/blue</item> 
</style> 

然後在你的清單,應用主題只有Activity

<activity android:name="com.example.app.BlueActivity" 
    android:theme="@style/BlueActivityTheme"/> 
0

在/res/value/styles.xml只要你想,你可以定義爲許多樣式,然後在根您可以使用的活動佈局xml項目:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
style="@style/MySecondStyle" 
.... 

樣式也可以在清單中進行分配。

即使您可以更改樣式只有一個視圖或一個ViewGroup中,採用主題的屬性,例如:

<TextView  
     android:theme="@style/MyThirdStyle" 
     ..... 
0

你必須創建自己的主題。請注意,我在styles.xml中命名爲MyTheme,並將colorPrimaryDark設置爲lightGreen

<resources> 

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    </style> 

    <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimaryDark">@color/lightGreen</item> 
    </style> 

</resources> 

現在的manifest.xml,你必須設置你的主題上activity標籤。不要在application標籤中設置您的主題。

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" > 

    <activity android:name=".MainActivity" 
     android:theme="@style/AppTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".OtherActivity1" 
     android:theme="@style/MyTheme" /> 
    <activity android:name=".OtherActivity2" 
     android:theme="@style/AppTheme" /> 
    <activity android:name=".OtherActivity3" 
     android:theme="@style/AppTheme" /> 

</application> 

現在,你可以看到我設置的自定義主題是MyThemeOtherActivity1。對於其餘的活動,我將主題設置爲默認主題。希望能幫助到你。

相關問題