我在Android库项目中有一个自定义的PieTimer视图
package com.mysite.android.library.pietimer;
public class PieTimerView extends View {
...
Run Code Online (Sandbox Code Playgroud)
我还有一个XML属性文件,我在PieTimer中使用它
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.PieTimerView);
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我在一个布局文件中有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>
Run Code Online (Sandbox Code Playgroud)
以前我用的xmlns:app是属性的命名空间,app:ok_color但当我把我的项目作为一个库项目并且有一个实现库的Free和Full版本时,我发现它不再有效,所以我添加了pie命名空间.
PieTimerView显示但属性不起作用,它们使用默认值(之前没有发生),因此这里存在一些名称空间冲突.
这样做的正确方法是什么?
JRa*_*ond 92
我知道这篇文章超级老了,但如果有人来看,这个问题已在ADT修订版17+中得到修复.任何服务或活动都将名称空间声明为:
xmlns:custom="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)
res-auto将在构建时使用实际项目包进行替换,因此请确保设置属性名称以尽可能避免冲突.
参考:http://www.androidpolice.com/2012/03/21/for-developers-adt-17-sdk-tools-r17-and-support-package-r7-released-with-new-features-and-修复换不起毛的构建系统和仿真器/
在与同事一起工作两个小时之后,我们想出了如何完成这项工作:
图书馆:com.library
mywidget.java与一个类 MyWidget
attrs.xml:
<declare-styleable name="MyWidget">
Run Code Online (Sandbox Code Playgroud)
并把你的attr.
您可以在库的布局文件夹中创建my_widget.xml,而不是在其中声明窗口小部件的布局(例如main.xml).
my_widget.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.library.MyWidget
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:widget="http://schemas.android.com/apk/res/com.library"
android:id="@+id/your_id"
android:layout_width="fill_parent" android:layout_height="fill_parent"
widget:your_attr>
</com.library.MyWidget>
Run Code Online (Sandbox Code Playgroud)
将它包含在你的布局中,就这么说吧
<include layout="@layout/my_widget"></include>
Run Code Online (Sandbox Code Playgroud)
App:com.myapp
您只需在此复制my_widget.xml,并更改命名空间:
<?xml version="1.0" encoding="utf-8"?>
<com.library.MyWidget
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:widget="http://schemas.android.com/apk/res/com.myapp" <---- VERY IMPORTANT
android:id="@+id/your_id"
android:layout_width="fill_parent" android:layout_height="fill_parent"
widget:your_attr>
</com.library.MyWidget>
Run Code Online (Sandbox Code Playgroud)
通常它会工作!
xmlns:app="http://schemas.android.com/apk/res/com.mysite.android.library.myapp"
Run Code Online (Sandbox Code Playgroud)
也许您指定的包(com.mysite.android.library.myapp)是错误的地方。您可以在 AndroidMinifest.xml 文件中检查 minifest 元素的 package 属性。它们应该是相同的。
| 归档时间: |
|
| 查看次数: |
19978 次 |
| 最近记录: |