如何让用户在您的应用程序中切换不同颜色的皮肤

Nul*_*ion 7 android android-styles android-color

我想让用户有机会选择颜色皮肤将有应用程序.例如,我应该使用具有不同色调的黑色皮肤以及具有色调的白色皮肤来实现黑色皮肤.

哪个是实现这一目标的最佳方法?

例如,我知道Android中有一个colors.xml文件,应该包含应用程序的颜色.是一种拥有两种颜色文件或某种东西的方法,并根据用户选择使用一种或另一种颜色?也许是styles.xml文件?

请告诉我您对实现这一目标的最佳方法的看法

谢谢

我的布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <RelativeLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.view.PagerTitleStrip
                android:id="@+id/pager_title_strip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:paddingTop="4dp"
                android:paddingBottom="4dp"
                style="@style/CustomPagerTitleStrip"/>
        </android.support.v4.view.ViewPager>
    </RelativeLayout>
    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu"
        style="@style/NavigationDrawerStyle"/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

我的styles.xml文件:

<resources>
    <style name="AppTheme" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="actionBarTheme">@style/CustomActionBar</item>
        <item name="android:textColor">@color/colorFontMenus</item>
        <item name="itemTextColor">@color/colorFontMenus</item>
        <item name="itemIconTint">@color/colorFontMenus</item>
    </style>

    <style name="CustomActionBar">
        <!-- title text color -->
        <item name="android:textColorPrimary">@color/colorFontMenus</item>
        <!-- subtitle text color -->
        <item name="android:textColorSecondary">?android:textColorPrimary</item>
        <!-- action menu item text color -->
        <item name="actionMenuTextColor">?android:textColorPrimary</item>
        <!-- action menu item icon color - only applies to appcompat-v7 icons :( -->
        <item name="colorControlNormal">?android:textColorPrimary</item>
    </style>

    <style name="CustomPagerTitleStrip">
        <item name="android:background">@color/mainColorWithAlpha</item>
    </style>

    <style name="CustomTextView">
        <item name="android:textColor">@color/colorFontContent</item>
    </style>

    <style name="CustomScrollBar">
        <item name="android:scrollbarThumbVertical">@drawable/scrollbar_green</item>
    </style>

    <style name="CustomDrawerHeader">
        <item name="android:background">@drawable/drawer_header_background_green</item>
    </style>

    <style name="NavigationDrawerStyle">
        <item name="android:background">@color/colorPrimaryDark</item>
   </style>

</resources>
Run Code Online (Sandbox Code Playgroud)

Joa*_*Ley 1

这应该在 styles/AppTheme.xml 中完成

\n\n

它\xe2\x80\x99s只能在方法中更改活动的主题onCreate(),并且只能在super()调用之前更改。更改主题相当简单,如下所示:

\n\n

setTheme(someBooleanFlag ? R.style.AppThemeOne : R.style.AppThemeTwo);

\n\n

在这里阅读更多内容:

\n\n

Ali Muzaffar 的 Medium 帖子

\n\n

风格和主题

\n\n

材质主题

\n