Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改操作栏_Java_Android_Android Actionbar_Colorbar - Fatal编程技术网

Java 更改操作栏

Java 更改操作栏,java,android,android-actionbar,colorbar,Java,Android,Android Actionbar,Colorbar,我想创建一个包含更多按钮的新活动。每个按钮代表一种颜色。如果单击表示颜色(例如红色)的按钮,如何更改操作栏(并保存) 我做了一些改变动作栏颜色的事情,但是如果我打开主页,颜色会再次改变。我需要序列化一些东西吗?谢谢 pink.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ getSupportActionBar().setTitle("Pink"); getSupport

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

我做了一些改变动作栏颜色的事情,但是如果我打开主页,颜色会再次改变。我需要序列化一些东西吗?谢谢

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>

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

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

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

onclick(){
SharedPreferences.Editor editor = sharedPreferences.edit();
            editor = sharedPreferences.edit();
            editor.putString("text","YOUR_TEXT");
            editor.putInt("color","YOUR_COLOR");
            editor.commit();
}
如果您在保存共享首选项中的颜色时仍然面临任何问题,您可以看到

阅读第一段。如文件中所述

<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>

@样式/MyActionBar
任何十六进制颜色代码

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

单击按钮颜色变换器

int color = R.color.colorPink;

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(color)));
并单击save 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)));

你的xml代码在哪里?现在我只有一个按钮…如果我单击,操作栏将是粉红色的,但只是临时的
,非常感谢!工作,但我想改变项目中所有动作栏的颜色。你有主意吗?我怎么做?(主页、设置等)。非常感谢。谢谢!工作,但我想改变项目中所有动作栏的颜色。你有主意吗?我怎么做?(主页、设置等)。谢谢您可以使用intent.putextra()将颜色值和标题作为意图进行paas,并在下一个活动中获取意图值。因此,假设您想从主页转到设置,然后在主页活动中启动设置意图,并使用intent.putextra()发送颜色值和标题文本,在设置活动onCreate()中,您可以使用getIntent().getExtra查看此处的帖子,了解intent.putextra()的工作原理。
SharedPreferences preferences = getSharedPreferences ("prefKey", Mode_Private);

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

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