我在 Android 库项目中有一个自定义 PieTimer 视图
package com.mysite.android.library.pietimer;
public class PieTimerView extends View {
...
我还有一个在 PieTimer 中使用的 XML 属性文件
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.PieTimerView);
XML 样式文件如下所示
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PieTimerView">
<attr name="max_size" format="dimension" />
<attr name="start_angle" format="string" />
<attr name="start_arc" format="string" />
<attr name="ok_color" format="color" />
<attr name="warning_color" format="color" />
<attr name="critical_color" format="color" />
<attr name="background_color" format="color" />
</declare-styleable>
</resources>
我在使用该库的项目的布局文件中有 PieTimer,如下所示
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root" android:layout_gravity="center_horizontal"
xmlns:app="http://schemas.android.com/apk/res/com.mysite.android.library.myapp"
xmlns:pie="com.mysite.library.pietimer"
>
<com.mysite.library.pietimer.PieTimerView
pie:start_angle="270"
pie:start_arc="0"
pie:max_size="70dp"
pie:ok_color="#9798AD"
pie:warning_color="#696DC1"
pie:critical_color="#E75757"
pie:background_color="#D3D6FF"
android:visibility="visible"
android:layout_height="wrap_content" android:id="@+id/clock"
android:layout_width="wrap_content"
android:layout_alignParentRight="true">
</com.mysite.library.pietimer.PieTimerView>
以前我使用的是xmlns:app
作为属性的命名空间,例如app:ok_color
但是当我将我的项目设为库项目并有一个实现该库的免费和完整版本时,我发现它不再起作用,因此我添加了pie命名空间。
PieTimerView 显示,但属性不起作用,它们使用默认值(以前没有发生过),因此这里发生了一些命名空间冲突。
这样做的正确方法是什么?