android - 以编程方式创建小部件

标签 android android-widget remoteview

据我了解,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/

相关文章:

android - Android 的“全屏”菜单

android - 多个 View 的 state_pressed 背景,onClick

Android-Widget : Changing ListView selector color using RemoteView

android - 在通知中使用 RatingBar

java - 你如何在Android中获得手机的MCC和MNC?

android - FloatingActionButton + VectorDrawable 在 pre-Lollipop 上崩溃

java - 为什么我的 GridView 在末尾中断?

Android - 小部件上的动画

android - 将自定义可打包对象额外或在 ArrayList 中传递给 RemoteViewsService 会中断 appwidget

android - Jetpack Navigation Deep Link 与错误的 ActionBar 按钮