Android Studio's “expected resource of type” checks?

前端 未结 4 1134
小鲜肉
小鲜肉 2020-12-05 02:29

Android Studio Beta (0.8) has a nifty new feature where it checks that some int parameters are not arbitrary integers, but rather have some properties.

4条回答
  •  抹茶落季
    2020-12-05 03:12

    (Thanks to @CommonsWare for the heads up).

    There are Java annotations to support these checks in your own code. They can all be found in the android.support.annotations package:

    • IdRes
    • DrawableRes
    • LayoutRes
    • StringRes
    • &c

    In this case, for example, I could use:

    private void mySetContentView(@LayoutRes int resourceId) {
        setContentView(resourceId);
    }
    

    and Android Studio will check that the provided resource id is indeed for a layout.

    Moreover, these annotations are exported, so they can be especially useful when designing a library.

    Sources:

    • Video from Google I/O 2014: What's new in Android development tools

提交回复
热议问题