2015-11-06 57 views
-1

我使用的是Activity,它的延伸范围为AppCompatActivity。我试图从微调器中的颜色列表中选择一种颜色,并将该颜色应用于工具栏。我怎样才能实现这个编程。Android更改工具栏颜色从Google列表中选择颜色颜色 - 附加截图

我的工具栏是:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:fitsSystemWindows="true" 
    android:minHeight="?attr/actionBarSize" 
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

我minSDK是16和CompileSDK为23

请看截图; enter image description here

enter image description here

回答

0

由于工具栏,你可以简单地改变它的使用背景图。

toolbar.setBackgroundColor(Color.parseColor("hex value of user selected color primary")); 

并用它来着色状态栏。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 

     Window window = getWindow(); 
     window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 

     window.setStatusBarColor(Color.parseColor("hex value of user selected color primary dark")); 
    } 

将用户偏好的颜色存储在存储首选项中。在启动时加载它,并使用它从每个活动的onCreate()方法着色工具栏。