run android lint value string 出错,android - Resolving resource values in custom lint rule - Stack Ove...

I have a large Android codebase and I am writing a custom lint rule that checks whether the values of certain attributes fall within a given range.

For example, I have this component:

my:animation_factor="0.7"

...>

and I want to write a lint rule that alerts developers that values of my:animation_factor >= 1 should be used with caution.

I followed the instructions at http://tools.android.com/tips/lint-custom-rules and managed to retrieve the value of my:animation_factor using this code:

import com.android.tools.lint.detector.api.*;

public class XmlInterpolatorFactorTooHighDetector {

....

@Override

public Collection getApplicableElements() {

return ImmutableList.of("MyCustomComponent");

}

@Override

public void visitElement(XmlContext context, Element element) {

String factor = element.getAttribute("my:animation_factor");

...

if (value.startsWith("@dimen/")) {

// How do I resolve @dimen/xyz to 1.85?

} else {

String value = Float.parseFloat(factor);

}

}

}

This code works fine when attributes such as my:animation_factor have literal values (e.g. 0.7).

However, when the attribute value is a resources (e.g. @dimen/standard_anim_factor) then element.getAttribute(...) returns the string value of the attribute instead of the actual resolved value.

For example, when I have a MyCustomComponent that looks like this:

my:animation_factor="@dimen/standard_anim_factory"

...>

and @dimen/standard_anim_factor is defined elsewhere:

1.85

then the string factor becomes "@dimen/standard_anim_factor" instead of "1.85".

Is there a way to resolve "@dimen/standard_anim_factor" to the actual value of resource (i.e. "1.85") while processing the MyCustomComponent element?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值