据我了解,android 小部件需要 RemoteViews,而不是 View,就像 Activity 一样。
我的问题:有什么办法可以完全避免xml,用java代码构建整个布局?
替代问题:我能否以某种方式检索 View ,就像我使用 findViewById(int)
一样?例如:LinearLayout linearLayout=(LinearLayout)findViewById(R.id.xxx);
这样我就可以访问基本布局,并根据需要进行操作。
谢谢!
最佳答案
is there any way to completely avoid xml, and build the whole layout with java code?
不,对不起。 RemoteViews
在很大程度上依赖于布局 XML 资源。虽然有一个 addView()
方法,但它会转身添加另一个 RemoteViews
,这会让您回到起点。
can I somehow retrieve a View, like I would use findViewById(int)?
没有,因为你的进程中没有View
。您可以通过 RemoteViews
上的各种设置方法配置小部件。
关于android - 以编程方式创建小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19116082/