哪个是实现这一目标的最佳方法?
例如,我知道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" /> </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" /></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>
更改主题时:
@OverrIDeprotected 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.如果你想看看它怎么样我可以给你一个我的应用程序的视频.
颜色来自:Material Color Palette
无论如何我知道2种不同的方法,如果你喜欢其中之一,我可以解释更多.
@Edit:好消息,我发现这个Github回购:app-theme-engine,它的工作正常.你可以试试apk.如果你不能通过gradle导入它,请尝试:compile’com.github.naman14:app-theme-engine:0.5.1@aar’
总结以上是内存溢出为你收集整理的android – 如何让用户在你的应用程序中切换不同颜色的皮肤全部内容,希望文章能够帮你解决android – 如何让用户在你的应用程序中切换不同颜色的皮肤所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)