2017-01-24 3 views
0

In meiner Android App habe ich zwei verschiedene Themen (hell und dunkel). Zum Beispiel:Verschiedene Thema Stile für nicht alle Ansichten

<style name="AppThemeDark" parent="Theme.AppCompat"> 
     <item name="colorPrimary">@android:color/black</item> 
     <item name="colorPrimaryDark">@android:color/black</item> 
     <item name="colorAccent">@android:color/holo_red_dark</item> 
     <item name="android:textColor">@android:color/white</item> 
     <item name="windowActionModeOverlay">true</item> 
</style> 

<style name="AppThemeLight" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="windowActionModeOverlay">true</item> 
</style> 

So, jetzt kann ich mich bewerben, beispielsweise unterschiedliche Textfarben auf eine Textview (wissen für dunkles Thema und schwarz für Licht):

<item name="android:textViewStyle">@style/TextViewDark</item> 

<style name="TextViewDark"> 
     <item name="android:textColor">?android:attr/colorAccent</item> 
</style> 

Aber es gilt zu alle TextViews.

Die Hauptfrage ist es möglich, in XML (nicht programmatisch) neben machen:

Licht Thema: Die Hälfte der Textviews Textfarbe schwarz, und ein weiteres halbes grün. Black Theme: TextViews, die schwarz in Light-Thema - rot, und eine andere Hälfte - blau (die in Light-Thema grün sind).

Antwort

0

2 Klassen erstellen definieren dont müssen sich TextView

public class OneTextView extends TextView { 

    public OneTextView(Context context) { 
     super(context); 
     init(context); 
    } 

    public OneTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public OneTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
    } 

    private void init(Context context){ 
     int[] attrs = new int[] { R.attr.myFirstColor}; 
     TypedArray ta = context.obtainStyledAttributes(attrs); 
     int appColor = ta.getColor(0, 0); 
     ta.recycle(); 

     // set theme color 
     setTextColor(appColor); 
    } 
} 

public class SecondTextView extends TextView { 

    public SecondTextView(Context context) { 
     super(context); 
     init(context); 
    } 

    public SecondTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public SecondTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
    } 

    private void init(Context context){ 
     int[] attrs = new int[] { R.attr.mySecondColor}; 
     TypedArray ta = context.obtainStyledAttributes(attrs); 
     int appColor = ta.getColor(0, 0); 
     ta.recycle(); 

     // set theme color 
     setTextColor(appColor); 
    } 
} 

jede Klasse, die Sie in XML-ähnliche

<com.route.to.class.OneTextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

OneTextView haben können Farben

SecondTextView haben grüne und blaue Farben

definieren attr.xml in values

schwarz und rot können verwenden können
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="myFirstColor" format="color" /> 
    <attr name="mySecondColor" format="color" /> 
</resources> 

Dann in Ihrem styles.xml definieren Farben für jedes Thema:

<style name="Theme.MyApp" parent="@style/Theme.Light"> 
    <item name="myFirstColor">@color/black</item> 
    <item name="mySecondColor">@color/green</item> 
</style> 

<style name="Theme.MyApp.Dark" parent="@style/Theme.Dark"> 
    <item name="myFirstColor">@color/green</item> 
    <item name="mySecondColor">@color/blue</item> 
</style> 
+0

Danke! Ich wusste nicht, dass es möglich ist, eigene Attribute zu erstellen. Genau das habe ich gesucht. – kara4k

0

Sie definiert haben, in styles.xml

<style name="TextViewDark"> 
    <item name="android:textColor">?android:attr/colorAccent</item> 
</style> 

<style name="TextViewLight"> 
    <item name="android:textColor">@color/green</item> 
</style> 

dann können Sie es in main.xml

verwenden
<LinearLayout> 

    <TextView 
     android:id="@+id/light_text_view" 
     android:text"i´m use light theme" 
     style="@style/TextViewLight"/> 

    <TextView 
     android:id="@+id/dark_text_view" 
     android:text"i´m use darktheme" 
     style="@style/TextViewDark"/> 

</LinearLayout> 

Sie Stile in AppThemes

+0

nicht vollständig verstanden. Können Sie etwas mehr Details angeben? – kara4k

+0

Aber in diesem Fall, wenn ich meine App-Thema von hell nach dunkel wechseln, TextViews Stile immer noch gleich, oder? Der erste Fernseher hat einen Farbakzent und der zweite eine grüne Farbe. Und sie werden nicht von dem Thema abhängen, das ich wähle (hell oder dunkel). – kara4k

Verwandte Themen