Android 如何仅为一项活动更改ColorPrimaryDark?
我只想为一项活动更改ColorPrimaryDark的颜色。我该怎么做 我想转到styles.xml并更改以下内容:Android 如何仅为一项活动更改ColorPrimaryDark?,android,kotlin,Android,Kotlin,我只想为一项活动更改ColorPrimaryDark的颜色。我该怎么做 我想转到styles.xml并更改以下内容: <item name="colorPrimaryDark">@color/blue</item> @color/blue 但问题是如果我这样做,我会改变我所有活动的颜色,我只需要改变一个活动的颜色 谢谢你的帮助 具体来说,这个颜色是应用程序顶部的颜色,我是指上面的ActionBar。我使用Kotlin来完成此操作。在活动中添加以下代码,以编程方式设置状
<item name="colorPrimaryDark">@color/blue</item>
@color/blue
但问题是如果我这样做,我会改变我所有活动的颜色,我只需要改变一个活动的颜色
谢谢你的帮助
具体来说,这个颜色是应用程序顶部的颜色,我是指上面的
ActionBar
。我使用Kotlin来完成此操作。在活动中添加以下代码,以编程方式设置状态栏颜色:
getWindow.setStatusBarColor(getResources().getColor(R.color.your_color));
此外,您还可以通过在style.xml
文件中写入主题来设置状态栏颜色:
<style name="YourActivityTheme" parent="AppTheme">
<item name="colorPrimaryDark">@color/yourColor</item>
</style>
@颜色/你的颜色
然后在清单文件中,您必须添加以下代码:
<activity android:name="packageName.YourActivity"
android:theme="@style/YourActivityTheme"/>
专门针对该活动
<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>
在/res/value/styles.xml中,可以定义任意多个样式,然后在活动布局xml的根项上可以使用:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/MySecondStyle"
....
您必须创建自己的主题。请注意,我在style.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>
现在,正如您所看到的,我已经在OtherActivity1
上设置了自定义主题,即MyTheme
。对于活动的其余部分,我将主题设置为默认主题。希望有帮助。这可能会启发你:你能发布你的styles.xml吗?你可以定义一种新的样式,将其父样式定义为应用程序中的常用样式,并在其中重新定义colorPrimaryDark。我正在为minSdkVersion 19和targetSdkVersion 26编写一个应用程序。manifest.xml中的activity元素没有“android:theme”属性。有什么解决办法吗?好的。尽管IDE抱怨没有“android:theme”,但它在应用程序中运行良好。谢谢。getWindow()方法的超类是什么?这里是红色的。
<TextView
android:theme="@style/MyThirdStyle"
.....
<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>
<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>