【发布时间】:2016-11-15 21:27:42
【问题描述】:
我想让用户有机会选择应用程序的颜色皮肤。例如,我应该实现具有不同色调的黑色的黑色皮肤和具有不同色调的白色皮肤。
实现这一目标的最佳方法是什么?
例如,我知道 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>
我的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>
【问题讨论】:
-
我没有发布正确答案的所有详细信息,只是为了提供指导:将其与样式/主题一起使用。在这里你可以找到如何在运行时在它们之间进行更改stackoverflow.com/questions/19125163/…
-
@NullPointerException 实际上我正在我的应用程序中执行此操作,但我不会更改整个主题。我只是根据
SharedPreferences中的颜色值以编程方式更改Statusbar颜色、Toolbar颜色、Fab 颜色和任何其他我想要的颜色。如果你愿意,我可以给你我的代码。但是如果你想检查一下我可以建议你看看这个:android-change-app-theme-runtime -
@NullPointerException 我已经写了一个关于如何得到这个的解释,还发布了一些资源,这样你就可以对这个问题有更广泛的了解。
标签: android android-styles android-color