所以我有一个 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 个选项执行它:
- 使用默认颜色集声明我自己的主题,用户将需要从我的继承他的主题。
- 在我的 color.xml 中添加一些 default_color,以便用户能够将其用作主题中的颜色。
第一个真的很糟糕,因为我不能强迫用户使用特定的应用程序主题(如 AppCompact.Light)。第二个也不是很好,因为我希望用户能够无缝地使用默认设置,并且我在主题中有几种颜色需要他设置。
您认为有没有其他方法可以让其他用户轻松使用这些颜色?
谢谢。