如何更改Apptheme?

时间:2018-07-28 14:31:53

标签: android android-studio

Image error我有2个样式文件。通过单击按钮,我想从AppTheme切换到AppTheme2。怎么做?

请注意,原色带有代码colorPrymary2。那会完全改变我的应用程序的颜色,这就是我想要的。

Manifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">

styles.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat">
        <item name="android:windowBackground">@drawable/background</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme2" parent="Theme.AppCompat">
        <item name="android:windowBackground">@drawable/background</item>
        <item name="colorPrimary">@color/colorPrimary2</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources> 

1 个答案:

答案 0 :(得分:1)

只需使用onCreate()方法即可!

getApplication().setTheme(R.style.yourtheme);