【问题标题】:Different theme styles for not all views并非所有视图都有不同的主题样式
【发布时间】:2017-06-09 19:35:48
【问题描述】:

在我的 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>

但它适用于所有 TextView。

主要问题,是否可以在 XML(不是以编程方式)下制作:

浅色主题:一半的 TextViews 文本颜色为黑色,另一半为绿色。 黑色主题:在 Light 主题中黑色的 TextView - 红色,另一半 - 蓝色(在 Light 主题中为绿色)。

【问题讨论】:

    标签: android android-theme android-styles


    【解决方案1】:

    创建 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 可以有绿色和蓝色

    values中定义attr.xml

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

    【讨论】:

    • 谢谢!我不知道可以创建自己的属性。这正是我想要的。
    【解决方案2】:

    你已经在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 中定义样式

    【讨论】:

    • 不完全理解。你能提供更多细节吗?
    • 但是,在这种情况下,当我将应用主题从浅色切换到深色时,TextView 的样式仍然相同,对吧?第一台电视将具有色彩重音,第二台电视将具有绿色。而且它们不会取决于我选择的主题(浅色或深色)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2015-03-29
    • 1970-01-01
    相关资源
    最近更新 更多