帮助自定义视图属性里面的Andr​​oid库项目自定义、视图、属性、里面

2023-09-12 02:42:23 作者:无言

我有一个自定义PieTimer查看在Android库项目

I have a custom PieTimer View in an Android Library Project

package com.mysite.android.library.pietimer;

public class PieTimerView extends View {
...

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

I also have a XML attributes file which I use in my PieTimer

    TypedArray a = context.obtainStyledAttributes(attrs,
        R.styleable.PieTimerView);

该XML设置样式文件看起来像这样

The XML styleable file looks like this

<?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为使用该库,这样一个项目布局文件

I have the PieTimer in a layout file for a project that uses the library, like this

<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>

previously我用的是的xmlns:应用程序作为命名空间的属性,如应用:ok_color 但当我做我的项目库项目,并有一个自由而全面的版本实现了图书馆,我发现它不再工作,所以我说馅饼命名空间。

Previously I was using the xmlns:app as the namespace for attributes like app:ok_color but when I made my project a library project and has a Free and Full version implementing the library, I found it no longer worked so I added the pie namespace.

在PieTimerView显示,但由于没有工作,他们所使用的默认值(这是没有发生之前),所以有一定的空间冲突hapenning这里。

The PieTimerView displays but the attributed do not work, they use the default (this was not occurring before) so there is some namespace conflict hapenning here.

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

What is the correct way of doing this?

推荐答案

我知道这个帖子是超级老了,但如果有人来寻找,这个问题已被固定在ADT修订17+。任何服务或活动声明命名空间为:

I know this post is super old, but if anyone comes looking, this problem has been fixed in ADT revision 17+ . Any services or Activities declare the namespace as:

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

RES-自动将替换在构建时与实际项目包,所以一定要设置您的属性名称,以避免冲突,如果在所有可能的。

res-auto will be replaced at build time with the actual project package, so make sure you set up your attribute names to avoid collisions if at all possible.

REF: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/