2014-12-18 3 views
2

Я хочу установить цвет, нажатый элемент навигационного элемента моего приложения, с помощью настроек, но я не могу найти способ сделать это. Я создал список в моих предпочтениях с шестнадцатеричными цветовыми кодами в качестве значений, и я не знаю, как использовать значения в моем классе java. ПОМОГИТЕ!Android: выбор цвета темы из настроек

ответ

0

Попробуйте этот подход: сначала определить новые цвета в colors.xml в значениях, как это:

<color name="my_blue">#4FC3F7</color> 
<color name="my_yellow">#FDD835</color> 

Затем определить новую тему, как это (my_theme.xml):

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <style name="MyRandomTheme" parent="Theme.AppCompat.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/my_blue</item> 
     <item name="colorPrimaryDark">@color/my_blue</item> 
     <item name="android:navigationBarColor">@color/my_blue</item> 
    </style> 

</resources> 

Тогда посмотрим, как изменить цвет динамически:

Мой макет:

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Blue" 
    android:id="@+id/btnBlue" 
    android:layout_below="@+id/imageView" 
    android:layout_alignLeft="@+id/imageView" 
    android:layout_alignStart="@+id/imageView" 
    android:background="#162c69" /> 

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Yellow" 
    android:id="@+id/btnYellow" 
    android:layout_below="@+id/imageView" 
    android:layout_alignRight="@+id/imageView" 
    android:layout_alignEnd="@+id/imageView" 
    android:background="#9b8a09" /> 

<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Mix" 
    android:id="@+id/btnMix" 
    android:layout_below="@+id/imageView" 
    android:layout_centerHorizontal="true" 
    android:background="#920f08" /> 

Моя активность:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    this.setTheme(R.style.MyRandomTheme); 


    setContentView(R.layout.activity_main); 

    //Change theme to yellow 
    Button yellowButton = (Button) findViewById(R.id.btnYellow); 
    yellowButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (Build.VERSION.SDK_INT >= 21) { 
       getWindow().setNavigationBarColor(getResources().getColor(R.color.my_yellow)); 
       getWindow().setStatusBarColor(getResources().getColor(R.color.my_yellow)); 
      } 
     } 
    }); 

    //Change theme to blue 
    Button blueButton = (Button) findViewById(R.id.btnBlue); 
    blueButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (Build.VERSION.SDK_INT >= 21) { 
       getWindow().setNavigationBarColor(getResources().getColor(R.color.my_blue)); 
       getWindow().setStatusBarColor(getResources().getColor(R.color.my_blue)); 
      } 
     } 
    }); 

    //Change theme to mixture of blue and yellow 
    Button mixButton = (Button) findViewById(R.id.btnMix); 
    mixButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (Build.VERSION.SDK_INT >= 21) { 
       getWindow().setNavigationBarColor(getResources().getColor(R.color.my_blue)); 
       getWindow().setStatusBarColor(getResources().getColor(R.color.my_yellow)); 
      } 
     } 
    }); 

}

Результат:

result

Кроме того, для чтения из предпочтений и записи предпочтений попробовать это:

 //Write to preferences 
     String s = "this is a test."; 
     SharedPreferences prefs = this.getSharedPreferences("MyPrefs",this.MODE_PRIVATE); 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putString("A", s); 
     editor.apply(); 
     //Fetch from preferences 
     SharedPreferences prefs2 = this.getSharedPreferences("MyPrefs", this.MODE_PRIVATE); 
     if(prefs2 != null) { 
      String text2 = prefs.getString("A",""); 
      Log.d(LOG_TAG, "This is the string: "+text2); 
     } 

Вы можете передать свои параметры, такие как «R.color.my_yellow», и использовать его в своих предпочтениях, пока ваш пользователь не изменит его. Я надеюсь, что это помогает. Дайте мне знать, если у вас есть вопрос.

Смежные вопросы