最近在做一个项目需要根据不同的逻辑更改LinearLayout的背景,看了下文档中remoteviews的方法,只有setInt (int viewId,String methodName, int value)可以一用,但运行后会直接出错,提示android.widget.LinearLayout can't use method with RemoteViews:
setBackgroundResource(int),但是setVisibility( int value)却没有任何问题,这是怎么回事呢?查看源码后发现LinearLayout中并不是所有的方法可以被remoteviews反射调用的,仅仅只有几个注有@android.view.RemotableViewMethod的方法才能反射。看样子我只能通过其他途径做此效果了。
又发现个问题,appWiget中不能添加ListView,原因在于remoteviews中有这么一段:
/**
* This annotation indicates that a subclass of View is alllowed to be used
* with the {@link android.widget.RemoteViews} mechanism.
*/
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface RemoteView {
}
也就是说只有注有@RemoteView这个 annotation的类才能被加载。