【问题标题】:Create a homescreen widget gui programmatically以编程方式创建主屏幕小部件 gui
【发布时间】:2017-12-08 21:53:49
【问题描述】:

我正在创建一个天气应用程序,我也在创建一个主屏幕小部件。该小部件将根据应用程序的用户设置显示多个位置的天气。因为用户可以选择显示任意数量的位置,所以我不认为我可以使用 xml 布局,而是我认为我应该以编程方式创建 gui,但如何!

我找不到任何设置 gui 的方法(例如 setContentView 活动)或从 xml 中获取 gui(我有一个带有空线性布局的 xml,我认为我可以添加视图)

如何以编程方式创建小部件 gui?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    好的,我自己解决了.. 经过大量的试验和错误。我以为我在这里发布了我的解决方案。

    我创建了一个名为 entity.xml 的 xml.file,其中包含代表一个位置所需的视图。我还创建了另一个名为 widgetLayout.xml 的 xml 文件,其中包含一个空的 linearLayout。

    然后,当我想动态创建小部件布局时,我会这样做。

          RemoteViews main = new     RemoteViews(getApplicationContext().getPackageName(),      R.layout.widget_layout);
        main.removeAllViews(R.id.main);
    
    
        RemoteViews location = new RemoteViews(getApplicationContext().getPackageName(), R.layout.entity);
        location.setTextViewText(R.id.SpotName, "loc");
    
        RemoteViews location2 = new      RemoteViews(getApplicationContext().getPackageName(), R.layout.entity);
        spot1.setTextViewText(R.id.SpotName, "loc2");
    
        main.addView(R.id.main, location);
        main.addView(R.id.main, location2);
    
        int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
        AppWidgetManager.getInstance(getApplicationContext()).updateAppWidget(allWidgetIds[0], main);
    

    【讨论】:

    • 恭喜修复!如果可以,请确保将您的答案标记为“已接受”,以便其他有相同问题的人可以访问您的解决方案。干杯~
    猜你喜欢
    • 1970-01-01
    • 2019-10-17
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    相关资源
    最近更新 更多