改变行动吧

时间:2017-05-14 21:21:46

标签: java android android-actionbar colorbar

我想创建一个包含更多按钮的新活动。每个按钮代表一种颜色。如果单击代表颜色的按钮(例如红色),如何更改操作栏(并保存)?

我做了一些改变动作栏颜色的东西,但如果我进入主页,颜色会再次改变。我需要序列化某些东西吗?谢谢!

pink.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
    getSupportActionBar().setTitle("Pink");
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPink)));
    if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP){
        Window window =getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.colorAccent));

    }
}

});

按钮xml。

   <Button android:id="@+id/button_pink" android:layout_width="296dp" 
    android:layout_height="49dp" android:layout_marginTop="26dp" 
    android:background="@color/colorPink" android:text="pink" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" />
   </android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:1)

首先阅读documentation。如文档中所述。

<resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
       <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>
 <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar"> 
       <item name="android:background">ANY_HEX_COLOR_CODE</item>
</style>
</resources>

并且,将“MyTheme”设置为应用程序/活动的主题。

答案 1 :(得分:0)

这种情况正在发生,因为当您重新启动活动时,根据活动生命周期onReStart()然后调用onStart(),操作栏颜色将变为默认值。与在代码中一样,您可以在单击事件上设置操作栏颜色。

可能的解决方案是,在按钮单击事件中,您可以将颜色值保存在共享首选项中,然后在onCreate()中使用该共享首选项和颜色更改代码。这将使您的应用程序设置之前保存的颜色。

Android Lifecycle

Shared Preference

onCreate(){
sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
int color = sharedPreferences.getInt("color", "DEFAULT_COLOR");
String title = sharedPreferences.getString("text", "DEFAULT_TEXT");
getSupportActionBar().setTitle(title);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(USE_THE_COLOR_VALUE_HERE)));//
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP){
  Window window =getWindow();   
  window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  window.setStatusBarColor(getResources().getColor(R.color.colorAccent));

    }
}

然后在按钮中单击存储颜色和标题

onclick(){
SharedPreferences.Editor editor = sharedPreferences.edit();
            editor = sharedPreferences.edit();
            editor.putString("text","YOUR_TEXT");
            editor.putInt("color","YOUR_COLOR");
            editor.commit();
}

如果在共享偏好中保存颜色时仍然遇到任何问题,可以看到

How to store color value in SharedMemory?

答案 2 :(得分:0)

onClick按钮颜色转换器

int color = R.color.colorPink;

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(color)));

并保存onClick

SharedPreferences preferences = getSharedPreferences ("prefKey", MODE_PRIVATE);

SharedPreferences.Editor editor = preferences.edit();

editor.putInt ("colorValue", color);

editor.apply ();

<强>的onCreate

SharedPreferences preferences = getSharedPreferences ("prefKey", Mode_Private);

int color = preferences.getInt ("colorValue", 0);

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(color)));
相关问题