如何使用户可以在您的应用程序中的不同颜色的皮肤之间进行切换 [英] How to give the user the possibility to switch between different colors skin in your App
问题描述
我想让用户有机会选择肤色最深的皮肤,并拥有该应用程序.例如,我要实施黑色肤色不同色调的黑色皮肤,以及白色肤色具有色调的皮肤.
I want to give the user the opportunity to select wich color skin will have the app. For example, i whould implement a black skin with black colours with different tonalities and also a white skin with withe tonalities.
哪个人是实现这一目标的最佳方法?
Which whould be the best approach to achieve this?
例如,我知道Android中有一个colors.xml文件,其中应包含您应用程序的颜色.谁会是拥有两个颜色文件或其他内容并根据用户选择使用一个或另一个颜色的方法?也许是styles.xml文件?
For example i know that there is a colors.xml file in Android that should contain the colours of your app. Whould be a way to have two colors files or something and to use one or other depending of user selection? Maybe a styles.xml file?
请告诉我您的意见,关于谁应该是实现此目标的最佳方法
Please tell me your opinion about which whould be the best approach to achieve this
谢谢
我的布局:
<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文件:
My styles.xml file:
<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>
推荐答案
关于@Joaquim Ley的答案,我们可以在super.onCreate()
之前更改主题.
Regarding to @Joaquim Ley's answer we can change Theme before super.onCreate()
.
在我的应用程序中(工作中)我的styles.xml
:
In my app(at work) my styles.xml
:
这是我的默认主题
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorPrimary</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:colorFocusedHighlight">@color/colorPrimary</item>
<item name="android:colorControlNormal">@color/amber_300</item>
<item name="android:colorControlActivated">@color/amber_300</item>
<item name="android:colorControlHighlight">@color/colorControlHighlight</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="actionBarItemBackground">?attr/selectableItemBackground</item>
<item name="colorControlHighlight">@color/colorControlHighlight</item>
<item name="android:windowContentTransitions">true</item>
<!-- Customize your theme here. -->
</style>
这是我的绿色主题:
<style name="AppTheme.Green" parent="AppTheme">
<item name="colorPrimary">@color/green_500</item>
<item name="colorPrimaryDark">@color/green_700</item>
<item name="android:colorAccent">@color/green_300</item>
<item name="android:statusBarColor">@color/green_700</item>
<item name="android:colorFocusedHighlight">@color/green_500</item>
<item name="android:colorControlNormal">@color/green_300</item>
<item name="android:colorControlActivated">@color/green_300</item>
</style>
更改主题时:
@Override
protected void onCreate(Bundle savedInstanceState) {
mPrefs=getSharedPreferences(getResources().getString(R.string.preference_name),MODE_PRIVATE);
mEditor=mPrefs.edit();
mEditor.apply();
defaultColor=mPrefs.getInt(getResources().getString(R.string.default_color),0);
//you can do some switch case or if else of defaultColor and change theme
setTheme(R.style.AppTheme_Green);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_voice_record);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
这是我工作中的第一个应用程序,但是在我的个人应用程序中,我创建了BaseActivity
并创建了方法handleColor(int color,int darkcolor)
.但这不会改变整个主题,只是Toolbar
,StatusBar
,NavigationBar
和EditText
标记和下划线颜色:
This is my first app in work, but in my personal app i created a BaseActivity
and created a method handleColor(int color,int darkcolor)
. But this won't change entire theme just Toolbar
, StatusBar
, NavigationBar
and EditText
marker and underline colors:
public class BaseActivity extends AppCompatActivity {
public void handleColor(int color,int darkcolor){
//changing toolbar color
if(getSupportActionBar()!=null){
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
}
//if bigger than Api 21 change status bar color
if(isLolipop()){
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(darkcolor);
}
}
public boolean isLolipop(){
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
}
更改颜色时,我正在使用材质对话框"颜色选择器.示例apk: sample.apk 和Github
: 材料对话框.如果您想看看它的外观,我可以为您提供我的应用的视频.
When changing color i am using Material Dialogs Color Picker. Sample apk : sample.apk and Github
: material-dialogs. If you want to see how it looks like i can give you a video of my app.
以下颜色:材料调色板
无论如何,我知道两种不同的方法,如果您喜欢其中一种,我可以解释更多.
Anyways i know 2 different approach , if you like one of them i can explain more.
@Edit :
对您来说是个好消息,我发现了这个Github存储库: app-theme-engine 及其运作良好.您可以尝试示例apk.如果无法通过gradle导入,请尝试以下方法:compile 'com.github.naman14:app-theme-engine:0.5.1@aar'
@Edit :
Good news for you, I found this Github repo : app-theme-engine and its working good. You can try sample apk. If you can't import it via gradle try this : compile 'com.github.naman14:app-theme-engine:0.5.1@aar'
这篇关于如何使用户可以在您的应用程序中的不同颜色的皮肤之间进行切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!