当前位置: 代码迷 >> 综合 >> AndroidAnnotations——Injecting project Resources注入项目资源
  详细解决方案

AndroidAnnotations——Injecting project Resources注入项目资源

热度:54   发布时间:2023-12-12 00:01:11.0
AndroidAnnotation

目录(?)[+]

  1. Resources
    1. StringRes
    2. ColorRes
    3. AnimationRes
    4. DimensionRes
    5. DimensionPixelOffsetRes
    6. DimensionPixelSizeRes
    7. Other XXXRes

Resources

Since AndroidAnnotations 1.0

All @XXXRes annotations indicate that an activity field should be injected with the correspondingAndroid resource from your res folder. The resource id can be set in the annotation parameter, ie@StringRes(R.string.hello).所有的 @XXXRes 注解标识一个activity字段应该由和res 文件夹下对应的Android resource 注入。这个资源id可以在注解参数中设置,@StringRes(R.string.hello)

If the resource id is not set, the name of the field will be used. The field must not be private.假如没有设置资源id,将默认使用字段名

@StringRes

The @StringRes annotation can be used to retrieve string resources. @StringRes 注解用来检索string资源。

Usage example:用法:

@EActivity
public class MyActivity extends Activity {
         @StringRes(R.string.hello)String myHelloString;@StringResString hello;}

@ColorRes

The @ColorRes annotation can be used to retrieve color resources. @ColorRes 注解用来检索color资源。

Usage example:用法:

@EActivity
public class MyActivity extends Activity {
         @ColorRes(R.color.backgroundColor)int someColor;@ColorResint backgroundColor;}

@AnimationRes

@AnimationRes can be used to inject XmlResourceParser fields (not very useful) orAnimationfields (much more interesting).@AnimationRes 注解可以注入 XmlResourceParser 字段(不常用)或者Animation字段(这种方式有趣的多)。

Usage example:用法:

@EActivity
public class MyActivity extends Activity {
         @AnimationRes(R.anim.fadein)XmlResourceParser xmlResAnim;@AnimationResAnimation fadein;}

@DimensionRes

The @DimensionRes annotation can be used to retrieve dimension resources. @DimensionRes 注解用来检索dimension 资源。

Usage example:用法:

@EActivity
public class MyActivity extends Activity {
         @DimensionRes(R.dimen.fontsize)float fontSizeDimension;@DimensionResfloat fontsize;}

@DimensionPixelOffsetRes

The @DimensionPixelOffsetRes annotation can be used to retrieve dimension resources. Retrieves the dimension to its final value as an integer pixel offset. This is the same as @DimensionRes, except the raw floating point value is truncated to an integer (pixel) value. @DimensionPixelOffsetRes 注解用来检索dimension 资源。获得一个以整型的像素偏移量为最终值的dimension 。这个注解功能和@DimensionRes一样,除了原始浮点数被截断成一个整型(像素)值。

Usage example:用法:

@EActivity
public class MyActivity extends Activity {
         @DimensionPixelOffsetRes(R.string.fontsize)int fontSizeDimension;@DimensionPixelOffsetResint fontsize;}

@DimensionPixelSizeRes

The @DimensionPixelSizeRes annotation can be used to retrieve dimension resources. Retrieves the dimension to its final value as an integer pixel size. This is the same as @DimensionRes, except the raw floating point value is converted to an integer (pixel) value for use as a size. A size conversion involves rounding the base value, and ensuring that a non-zero base value is at least one pixel in size. @DimensionPixelSizeRes注解用来检索dimension 资源。获得一个以整型的像素大小为最终值的dimension 。这个注解功能和@DimensionRes一样,除了原始浮点数被截断成一个整型(像素)值作为大小值。大小转换涉及到基值舍入,确保一个非零的基值至少有1像素的大小。

Usage example:用法:

@EActivity
public class MyActivity extends Activity {
         @DimensionPixelSizeRes(R.string.fontsize)int fontSizeDimension;@DimensionPixelSizeResint fontsize;}

Other @XXXRes

Here is the list of other supported resource annotations: 这里是其他支持资源注解的列表:

  • @BooleanRes
  • @ColorStateListRes
  • @DrawableRes
  • @IntArrayRes
  • @IntegerRes
  • @LayoutRes
  • @MovieRes
  • @TextRes
  • @TextArrayRes
  • @StringArrayRes
  相关解决方案