2010-12-16 67 views
37

中的属性我有一个自定义PieTimer查看在Android库项目帮助与自定义视图中的Android库项目

package com.mysite.android.library.pietimer; 

public class PieTimerView extends View { 
... 

我也有一个XML属性,这是我在我的PieTimer使用文件

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,但是当我做我的项目库项目,并有一个自由而全面的版本实现了图书馆,我发现它的属性没有更长时间的工作,所以我添加了饼图命名空间

PieTimerView显示但属性不起作用,它们使用默认值(这在之前没有出现),所以这里有一些名称空间冲突。

这样做的正确方法是什么?

+0

您是否找到更好的解决方案? – 2011-01-25 15:26:12

+0

请告诉我有更好的方法来做到这一点。在包含图书馆项目中的admob广告时,我遇到同样的问题。 – 2011-02-13 23:45:39

+0

好问题。我也面临同样的问题。欢迎大家给点意见。 – 2012-09-25 07:20:46

回答

0
xmlns:app="http://schemas.android.com/apk/res/com.mysite.android.library.myapp" 

也许您指定的包(com.mysite.android.library.myapp)是错误的地方。你可以在AndroidMinifest.xml文件中检查minifest元素的package属性。他们应该是相同的。

+1

由于这是一个库项目(包含所有逻辑和布局),子项目有一个不同的命名空间,这导致了问题。像这样,'com.mysite.android.library.myapp.lite'和'com.mysite.android.library.myapp。升级' – jax 2010-12-17 04:34:17

+0

我通过手动将所有布局文件复制到子项目并更改命名空间来解决此问题......但这是一个非常糟糕的解决方案,尤其是当您有多个布局时。 – jax 2010-12-17 04:35:51

4

有一个已知的bug及其描述在这里:bug #9656。我还描述了为什么这发生在我的博客post

9

与我的同事工作两小时后,我们弄清楚如何使这项工作: 库:com.library mywidget.java带班进myWidget

attrs.xml:

<declare-styleable name="MyWidget"> 

并把你的属性

而不是你的布局(例如main.xml),其中声明的小部件,juste创建一个my_widget.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> 

为包括它在你的布局,中庸之道把

<include layout="@layout/my_widget"></include> 

应用: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> 

通常它会工作!

0

该解决方案为我工作:

https://gist.github.com/1105281

它不使用TypedArray和要求视图有库的项目包名称中它硬编码。

它也不使用程序化级别上的可修改值。您还必须将属性字符串硬编码到视图中。

但是,您应该声明那些相同的属性名称,以便在Lint编写XML部件时不显示错误。

链接到原来的职位上code.google.com:

http://code.google.com/p/android/issues/detail?id=9656#c17

它绝对胜过复制文件并更改命名空间,但它仍然是不优雅的应该是。请享用。

92

我知道这篇文章是超级老,但如果有人来看,这个问题已修复ADT修订版17+。任何服务或活动声明为命名空间:

xmlns:custom="http://schemas.android.com/apk/res-auto" 

RES-汽车将在构建时与实际项目包进行更换,所以一定要设置你的属性名称,以避免如果在所有可能的冲突。

裁判:http://www.androidpolice.com/2012/03/21/for-developers-adt-17-sdk-tools-r17-and-support-package-r7-released-with-new-features-and-fixes-for-lint-build-system-and-emulator/

+3

+1不错的镜头,原始链接在这里:[修订ADT 17.0.0](http://developer.android.com/sdk/eclipse-adt.html)。 – yorkw 2012-05-16 22:17:03

+1

问题:这对我们的构建目标有什么影响吗? (我猜不是,但我不太确定...) – ArtOfWarfare 2012-11-12 20:10:42

+2

@ArtOfWarfare它不应该是这个工具集的一部分,它会调整你的源代码。应该只关系你安装的工具的版本 – JRaymond 2012-11-12 20:12:50

1

对于所有的API,这是收据(如果库被链接为JAR,也将正常运行的Android库项目参考)

库项目: AndroidManifest .xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.myapp.library1" 
... 
</manifest> 

库项目: Attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources>  
<declare-styleable name="MyCustStyle" > 
     <attr name="MyAttr1" format="dimension" /> 
    .. 
</resources> 

应用项目 MainLayout.xml(或其中u使用自定义的控件的布局)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:library1="http://schemas.android.com/apk/com.myapp.library1" 
... 
> 

<com.myapp.library1.MyCustomView 
     library1:MyAttr1="16dp" 
... 
/> 
</RelativeLayout> 

在应用程序代码,如果你想使用资料库中获取资源(恩。数组“MyCustomArray”在lib.xml中定义的arrays.xml中)使用:

getResources().getStringArray(com.myapp.library1.R.array.MyCustomArray)