How can I change ColorPrimaryDark for just one act

2019-05-26 20:07发布

问题:

I would like to change the color of ColorPrimaryDark for just one activity. How can I do this?

I think go to the styles.xml and change this:

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

But the problem is if I do this I change the color of all my activities and I just need to change the color of one activity.

Thank you for your help!

To be specific, this color is the color of the bar at the top of the app I mean above of ActionBar. I use Kotlin to do this.

回答1:

Create a theme specifically for that 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>

Then in your manifest, apply the theme to only that Activity:

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


回答2:

You have to create your own theme. Notice that I hace a new theme in styles.xml named MyTheme and I set the colorPrimaryDark to 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>

Now in manifest.xml, you have to set your theme on activity tag. Do not set your theme in application tag.

<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>

Now, as you can see I have set custom theme which is MyTheme on OtherActivity1. For the the rest of the activity I set the theme as the default theme. Hope it helps.



回答3:

Add the below code in your activity to set the status bar color programmatically:

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

Also, you can set the status bar color by writing theme in styles.xml file:

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

Then in the manifest file, you have to add the below code:

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


回答4:

In /res/value/styles.xml you can define as many styles as you want, then on the root item of your activity layout xml you can use:

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

Style can be assigned in the manifest too.

Even you can change style for only one View or one ViewGroup, using theme attribute, for example:

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