ユーザがAlertDialog
からオプションを選択したときに、アプリのテーマや色を変更しようとしています。私はフォントのサイズを変更するための同じテクニックを作った、それはうまく動作します。私は色を変えるために私が作った間違いは何ですか?アプリのテーマは動的に変更されません
のstrings.xml:
<resources>
<string-array name="font_sizes">
<item>14</item>
<item>16</item>
<item>18</item>
<item>20</item>
<item>22</item>
</string-array>
<string-array name="app_colors">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
</resources>
のstyles.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">@color/colorPrimaryDark</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="BlackTheme">
<item name="colorPrimary">#FF000000</item>
<item name="colorPrimaryDark">#FF000000</item>
<item name="colorAccent">#FF000000</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">#FF000000</item>
</style>
<style name="BlueTheme">
<item name="colorPrimary">#FF0000FF</item>
<item name="colorPrimaryDark">#FF0000FF</item>
<item name="colorAccent">#FF0000FF</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">#FF0000FF</item>
</style>
<style name="GreenTheme">
<item name="colorPrimary">#008000</item>
<item name="colorPrimaryDark">#008000</item>
<item name="colorAccent">#008000</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">#008000</item>
</style>
<style name="MagentaTheme">
<item name="colorPrimary">#D500F9</item>
<item name="colorPrimaryDark">#D500F9</item>
<item name="colorAccent">#D500F9</item>
<item name="android:navigationBarColor" tools:targetApi="lollipop">#D500F9</item>
</style>
</resources>
PreferencesActivity.java:
package sa.edu.qu.coc.cocapps.prefs;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import sa.edu.qu.coc.cocapps.R;
public class PreferencesActivity extends AppCompatActivity implements View.OnClickListener {
private Toolbar toolbar = null;
public static final String PREFS_KEY = "preferences";
private static int item = 0, color = 0;
private LinearLayout fontSize;
private LinearLayout appColor;
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
initViews();
setSupportActionBar(toolbar);
fontSize.setOnClickListener(this);
appColor.setOnClickListener(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void initViews() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
fontSize = (LinearLayout) findViewById(R.id.fontSize);
appColor = (LinearLayout) findViewById(R.id.appColor);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.fontSize:
AlertDialog.Builder changeFontSizeDialog = new AlertDialog.Builder(this)
.setTitle("Select Size")
.setIcon(android.R.drawable.sym_action_chat)
.setSingleChoiceItems(R.array.font_sizes, item, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
editor = prefs.edit();
editor.putInt("fontSize", i);
editor.commit();
item = i;
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// DO not do anything.
}
});
changeFontSizeDialog.show();
break;
case R.id.appColor:
AlertDialog.Builder changeAppColorDialog = new AlertDialog.Builder(this)
.setTitle("Select color")
.setIcon(android.R.drawable.sym_action_chat)
.setSingleChoiceItems(R.array.app_colors, color, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editor = prefs.edit();
editor.putInt("appColor", which);
editor.commit();
color = which;
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// DO not do anything.
}
});
changeAppColorDialog.show();
break;
}
}
@Override
protected void onStart() {
super.onStart();
switch(prefs.getInt("appColor", 0))
{
case 0:
this.setTheme(R.style.AppTheme);
return;
case 1:
this.setTheme(R.style.BlackTheme);
return;
case 2:
this.setTheme(R.style.BlueTheme);
return;
case 3:
this.setTheme(R.style.GreenTheme);
return;
case 4:
this.setTheme(R.style.MagentaTheme);
return;
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}
テーマを表示するためにアクティビティを再開する必要があります – tyczj