10

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

颜色.xml:

<resources>
    <attr name="color" format="reference" />
</resources>

布局.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)。第二个也不是很好,因为我希望用户能够无缝地使用默认设置,并且我在主题中有几种颜色需要他设置。

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

谢谢。

4

3 回答 3

7

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

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

颜色.xml:

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

布局.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>

样式.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>
于 2016-01-27T13:50:17.903 回答
0

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

在您的 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"/>
于 2016-01-21T17:40:49.810 回答
0

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

于 2016-01-24T10:53:26.550 回答