2015-07-04 9 views
11

To mój pasek narzędzi xmlJak programowo zmienić motyw paska narzędzi AppCompat v21?

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 

    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    app:contentInsetEnd="0dp" 
    app:contentInsetStart="0dp" 
    android:layout_height="@dimen/toolbar_height" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    android:background="@color/primary_color"> 



</android.support.v7.widget.Toolbar> 

Chcę zmienić aplikację: motyw programowo. Jak to zrobić?

+0

Nie można zmienić styl i tematyka. Co chcesz robić? Może istnieje inne rozwiązanie. – krystian71115

+0

Dobrze, chcę pozwolić użytkownikom wybrać kolor paska narzędzi .... w zależności od koloru Chcę zmienić motyw .. więc jeśli wybierze ciemny motyw kolor będzie ThemeOverlay.AppCompat.Dark.ActionBar inaczej byłoby światło motyw –

+0

I link do odpowiedzi, jak ustawić kolor płytek: http://stackoverflow.com/a/26594674/2536878 – krystian71115

Odpowiedz

18

Można to zrobić programowo lub z klasą:

Toolbar toolbar; // your toolbar 
toolbar.setBackgroundColor(newColor); // i don't tested this method. Write if it's not working 
toolbar.setTitleTextColor(titleColor); // if toolbar is white set title to black, if toolbar is black set title to white 

Albo można zrobić to w dobrym stylu:

Dodaj attrs.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="toolbarStyle" format="reference"/> 
</resources> 

A teraz zmienić toolbar.xml :

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 

    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    app:contentInsetEnd="0dp" 
    app:contentInsetStart="0dp" 
    android:layout_height="@dimen/toolbar_height" 
    app:theme="?attr/toolbarStyle" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    android:background="@color/primary_color"> 



</android.support.v7.widget.Toolbar> 

I styles.xml (jeśli nie masz to utwórz go):

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MyStyle.Dark" parent="AppCompat.Theme"> 
     <item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item> 
    </style> 
    <style name="MyStyle.Light" parent="AppCompat.Theme.Light"> 
     <item name="toolbarStyle">@style/ThemeOverlay.AppCompat.Light.ActionBar</item> 
    </style> 
</resources> 

Jeśli wybierzesz drugą metodę (ze stylów) należy ponownie uruchomić działalność i wykorzystać metodę setTheme przed super.onCreate()

Mam nadzieję, że ci pomogłem.

+0

Witam, Krystian. Pierwszy sposób nie działa. Ale może teraz sposób w jaki sposób zmienić pasek narzędzi między fragmentami zastępującymi wewnątrz jednej instancji Activity? Trzeba uniknąć odtwarzania aktywności. Dzięki –

6

Użyj następującego fragmentu kodu, aby dodać motyw:

Toolbar toolbar; 
toolbar.getContext().setTheme(R.style.ThemeOverlay_AppCompat_Dark_ActionBar); 
+0

pracował dla mnie .. Dzięki –

Powiązane problemy