2017-01-24 11 views
0

В моем приложении для Android у меня есть две разные темы (светлые и темные). Например:Различные стили для не всех просмотров

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

Итак, теперь я могу применить, например, различные цвета текста в TextView (белый для темной темы и черный для света):

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

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

Но это будет применяться к все TextViews.

Главный вопрос, можно ли сделать в XML (не программно) следующий:

Light Тема: Половина цвета TextViews текста черный, а другая половина зеленого цвета. Черная тема: TextViews, что черная в свете тема - красный, а еще одна половина - синяя (которые являются зелеными в свете темы).

ответ

0

Создание 2 классов продолжается 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); 
    } 
} 

каждый класс, который вы можете использовать в XML, как этот

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

OneTextView может иметь черные и красные цвета

SecondTextView может иметь зеленый и синий цвета

определяют attr.xml в values

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="myFirstColor" format="color" /> 
    <attr name="mySecondColor" format="color" /> 
</resources> 

Затем в вашем styles.xml определите цвета для каждой темы:

<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

Спасибо! Я не знал, что можно создавать свои собственные атрибуты. Это именно то, что я искал. – kara4k

0

вы определили в 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> 

, то вы можете использовать его в main.xml

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

вам не нужно определить стили в AppThemes

+0

Непонятно. Можете ли вы предоставить немного больше деталей? – kara4k

+0

Но, в этом случае, когда я переключаю тему приложения из света в темноту, стили TextView все те же, верно? Первый телевизор будет иметь цветной акцент и второй зеленый цвет. И они не будут зависеть от темы, которую я выбираю (светлой или темной). – kara4k

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

  • Нет связанных вопросов^_^