将默认颜色设置为Android库

将默认颜色设置为Android库,android,android-view,android-custom-view,android-theme,android-custom-attributes,Android,Android View,Android Custom View,Android Theme,Android Custom Attributes,所以我有一个安卓系统,我希望其他人在使用它时能够轻松定制它的颜色。问题是颜色不仅仅是一个属性(如视图背景),更像是一个主题颜色(视图背景、文本颜色、按钮笔划等),因此我不能将其作为视图属性传递。因此,我最终使用了颜色参考,并将其用于样式、布局和绘图: colors.xml: <resources> <attr name="color" format="reference" /> </resources> <resources> &

所以我有一个安卓系统,我希望其他人在使用它时能够轻松定制它的颜色。问题是颜色不仅仅是一个属性(如视图背景),更像是一个主题颜色(视图背景、文本颜色、按钮笔划等),因此我不能将其作为视图属性传递。因此,我最终使用了
颜色参考
,并将其用于样式、布局和绘图:

colors.xml:

<resources>
    <attr name="color" format="reference" />
</resources>
<resources>
    <color name="color">#FFFFFF</color>
</resources>
<resources>
    <color name="color">#000000</color>
</resources>

layout.xml:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/color">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="?attr/color"/>
    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/LibraryTheme.TextAppearance"/>
</LinearLayout>
<LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/color">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/color"/>
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:theme="@style/LibraryTheme.TextAppearance"/>
</LinearLayout>

库项目中的styles.xml:

<style name="LibraryTheme.TextAppearance">
    <item name="android:textColor">?attr/color</item>
    <item name="colorControlNormal">?attr/color</item>
    <item name="android:textColorHint">?attr/color</item>
    <item name="colorControlActivated">?attr/color</item>
    <item name="colorControlHighlight">?attr/color</item>
</style>

?属性/颜色
?属性/颜色
?属性/颜色
?属性/颜色
?属性/颜色
这非常有效,当有人使用我的库时,他必须声明他希望你在他的主题中使用的颜色:

应用程序项目中的styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="color">@color/my_color</item>
</style>

@颜色/我的颜色
问题是:我想提供一个默认颜色方案的库。现在可以使用两个选项执行此操作:

  • 使用默认颜色集声明我自己的主题,用户需要从我的主题继承他的主题
  • 在my color.xml中添加一些默认颜色,以便用户能够将其用作主题中的颜色
  • 第一个很糟糕,因为我不能强迫用户使用特定的应用程序主题(比如AppCompact.Light)。 第二个也不是很好,因为我希望用户能够无缝地使用默认设置,并且我在主题中有一些颜色需要设置

    你认为我还有其他方法可以让其他用户轻松地使用颜色吗


    谢谢。

    我不确定我是否正确理解了这一点,但我想你有以下选择:

    在Strings.xml文件中声明如下内容:

    <!-- Colors-->
        <color name="color1">#55000000</color>
        <color name="color2">#FFFFFF</color>
        <color name="color3">#000000</color>
    
    
    #55000000
    #FFFFFF
    #000000
    
    然后自定义传递此引用的模板对象。如果用户想要更改所有应用程序颜色,只需在String.xml中设置新值即可

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/color1"/>
    
    
    
    我坚持选择1。由于主题只是属性的集合,我看不出你如何“强制”用户继承你的主题-他只需要提供属性列表,而且他有一些工作要做…

    问题是你使用的是颜色引用,在您的情况下,我将使用常规颜色属性-这些属性可以由用户在colors.xml中重写

    因此,在您的图书馆项目中:

    <style name="LibraryTheme.TextAppearance">
        <item name="android:textColor">?attr/color</item>
        <item name="colorControlNormal">?attr/color</item>
        <item name="android:textColorHint">?attr/color</item>
        <item name="colorControlActivated">?attr/color</item>
        <item name="colorControlHighlight">?attr/color</item>
    </style>
    
    colors.xml:

    <resources>
        <attr name="color" format="reference" />
    </resources>
    
    <resources>
        <color name="color">#FFFFFF</color>
    </resources>
    
    <resources>
        <color name="color">#000000</color>
    </resources>
    
    
    #FFFFFF
    
    layout.xml:

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/color">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="?attr/color"/>
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:theme="@style/LibraryTheme.TextAppearance"/>
    </LinearLayout>
    
    <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/color">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/color"/>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:theme="@style/LibraryTheme.TextAppearance"/>
    </LinearLayout>
    
    
    
    styles.xml:

    <style name="LibraryTheme.TextAppearance">
        <item name="android:textColor">@color/color</item>
        <item name="colorControlNormal">@color/color</item>
        <item name="android:textColorHint">@color/color</item>
        <item name="colorControlActivated">@color/color</item>
        <item name="colorControlHighlight">@color/color</item>
    </style>
    
    
    @颜色/颜色
    @颜色/颜色
    @颜色/颜色
    @颜色/颜色
    @颜色/颜色
    
    如果用户想要使用不同的颜色,他可以在应用程序的colors.xml中声明自己的颜色:

    <resources>
        <attr name="color" format="reference" />
    </resources>
    
    <resources>
        <color name="color">#FFFFFF</color>
    </resources>
    
    <resources>
        <color name="color">#000000</color>
    </resources>
    
    
    #000000
    
    TextView在库中,因此用户无法将颜色放在库中,我没有理解您的意思。您所指的此用户是程序员或安装了应用程序的人?抱歉,我们将尝试更清楚地说明-该用户是希望使用库的程序员。但他需要做的唯一一件事是将文本放在他的布局中(文本视图,所有内容都在库代码中),您可以检查项目的设置。这是相当复杂的,因为我必须提供很多主题选项。从你的分类来看,我认为这个设置应该属于1<代码>第一个非常糟糕,因为我无法强制用户使用特定的应用程序主题(如AppCopact.Light)——不,您将要求用户覆盖您的主题,如
    parent=“MyLibrarysDefaultTheme”
    。这没关系。@Vikram看起来像是您使用了所有样式属性作为单个属性,只是在代码中设置了它们(如
    iconColor
    ),如果我在多个地方设置了颜色,而其中一些无法在代码中设置,该怎么办?您需要的是控制设置了哪些属性,以及当属性不存在/无效时该怎么办。只有在创建自定义组件时才能提供此控件。在您的特定情况下,您将检查是否已设置
    attr/color
    ,如果未设置,则在运行时使用默认值。你应该记住的另一件事是,你的产品的用户(或者说客户)将是开发者,而不是最终用户。如果它没有从库的核心功能中拿走任何东西的话,就可以强加一个要求。非常感谢,应用程序资源文件中指定的颜色有点被覆盖。我一直在这样做,但我遇到了一个情况,我的一个用户要求根据登录的用户类型更改主题。现在不能在运行时更改颜色。