我想让用户有机会选择颜色皮肤将有应用程序.例如,我应该使用具有不同色调的黑色皮肤以及具有色调的白色皮肤来实现黑色皮肤.
哪个是实现这一目标的最佳方法?
例如,我知道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>
解决方法
关于@Joaquim Ley的回答,我们可以在super.onCreate()之前更改Theme.
在我的应用程序(工作中)我的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).但这不会改变整个主题只有工具栏,StatusBar,NavigationBar和EditText标记以及下划线颜色:
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:material-dialogs.如果你想看看它怎么样我可以给你一个我的应用程序的视频.
无论如何我知道2种不同的方法,如果你喜欢其中之一,我可以解释更多.
@Edit:好消息,我发现这个Github回购:app-theme-engine,它的工作正常.你可以试试apk.如果你不能通过gradle导入它,请尝试:compile’com.github.naman14:app-theme-engine:0.5.1@aar’