2017-07-31 48 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。对于其余的活动,我将主题设置为默认主题。希望能帮助到你。

相关问题