【问题标题】:Set default colors to Android library将默认颜色设置为 Android 库
【发布时间】:2016-04-28 02:11:24
【问题描述】:

所以我有一个 Android library,我希望其他人在使用它时能够轻松自定义它的颜色。问题是颜色不仅仅是一个属性(如视图背景),它更像是一种主题颜色(视图背景、文本颜色、按钮的笔触等......)所以我不能只将它作为视图属性传递。所以我最终使用了color reference 并将其用于样式、布局和可绘制对象:

colors.xml:

<resources>
    <attr name="color" format="reference" />
</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>

库项目中的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>

问题是这样的:我想使用默认配色方案提供这个库。现在可以通过 2 个选项执行:

  1. 使用默认颜色集声明我自己的主题,用户需要从我的主题继承他的主题。
  2. 在我的 color.xml 中添加一些 default_color,以便用户可以将其用作主题中的颜色。

第一个真的很糟糕,因为我不能强迫用户使用特定的应用程序主题(如 AppCompact.Light)。 第二个也不是很好,因为我希望用户能够无缝地使用默认设置,并且我在主题中有几种颜色需要他设置。

您认为有没有其他方法可以让其他用户轻松使用这些颜色?

谢谢。

【问题讨论】:

  • 您可以查看SublimePicker项目的theme设置。这很复杂,因为我必须提供很多主题选项。从你的分类来看,我认为这个设置属于 1。 The first one is really bad, cause I can't force the user to use specific app theme (like AppCopact.Light) --- 不,您会要求用户覆盖 your 主题,例如 parent="MyLibrarysDefaultTheme"。没关系。
  • @Vikram 看起来您将所有样式属性用作单个属性,并在代码中设置它们(如iconColor),如果我在多个地方有颜色,其中一些不能在代码中设置?
  • 您需要控制设置了哪些属性,以及当它们不存在/无效时该怎么做。只有在创建自定义组件时才能提供此控件。在您的特定情况下,您将检查是否已设置 attr/color,如果没有,请使用默认值 - 在 runtime。您应该记住的另一件事是您的产品的用户(或者更确切地说是客户)将是开发人员,而不是最终用户。如果它不会从库的核心功能中删除任何内容,则可以强加一个要求。

标签: android android-view android-custom-view android-theme android-custom-attributes


【解决方案1】:

问题是您使用的是颜色参考,在您的情况下,我会使用常规颜色属性 - 用户可以在 colors.xml 中覆盖这些属性。

所以在你的图书馆项目中:

colors.xml:

<resources>
    <color name="color">#FFFFFF</color>
</resources>

layout.xml:

<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>
    <color name="color">#000000</color>
</resources>

【讨论】:

  • 非常感谢,应用资源文件中指定的颜色有点被覆盖了。
  • 我一直在这样做,但我遇到了一种情况,我的一个用户需要根据登录的用户类型更改主题。现在无法在运行时更改颜色。
【解决方案2】:

我不确定我是否理解正确,但我猜你有以下选择:

在您的 Strings.xml 文件中声明如下:

<!-- Colors-->
    <color name="color1">#55000000</color>
    <color name="color2">#FFFFFF</color>
    <color name="color3">#000000</color>

然后自定义传递此引用的模板对象。如果用户想要更改所有应用程序的颜色,他只需要在 String.xml 中设置新值

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/color1"/>

【讨论】:

  • TextView 在库中,所以用户不能把颜色放在那里
  • 伙计,我没有得到你。您指的这位用户是程序员还是安装了该应用的人?
  • 抱歉,我会尽量让它更清楚 - 用户是想要使用该库的程序员。但他唯一需要做的就是将 放在他的布局中(文本视图和所有内容都在库代码中)
【解决方案3】:

我会坚持选项 1。由于主题只是属性的集合,我看不出您如何“强制”用户继承您的主题 - 他只需要提供属性列表,再加上他有一些工作要开始...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2013-10-26
    相关资源
    最近更新 更多