2010-12-16 72 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)