【问题标题】:Dynamically add TextViews to Widget将 TextView 动态添加到 Widget
【发布时间】:2013-01-14 20:24:19
【问题描述】:

我决定为我的应用程序制作一个小部件,但我遇到了一些问题!

我想根据arrayList的大小在widget上添加多个textviews,但是在google之后我终于发现这是不可能的。

虽然我尝试了很多解决方法,但似乎没有任何效果!

所以我想请你帮忙!即使使用预定义的 xml 文件也可以这样做吗?如何这样做?

所以我真正要寻找的是一段合适的代码来演示我上面描述的内容!或任何其他可以引导我解决问题的帮助!

提前感谢您的帮助!

/撞! 有人吗?

【问题讨论】:

  • 您可以循环遍历 arrayList 并在每次这样做时创建一个新的 TextView。然后只需将其添加到所需的视图中。要创建一个 TextView,只需使用: TextView tv = new TextView(context);然后将其添加到视图中,只需在所需视图上调用 .addView(tv)。
  • 好吧,我试过了,但它不起作用!这就是为什么我在我的问题中声明只允许预定义的 xml TextViews 但我不知道如何实现这样的事情!
  • 如果您想使用 XML 代码,请使用 Inflater
  • 你能提供一个有效的代码示例来实现这个吗?
  • 假设您有一个 XML 文件,其中包含您想要使用的 TextView。 LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);然后你可以在 inflater 对象上调用 inflate 来获取它作为一个视图,然后像往常一样将它添加到你的外部视图中。你可以看这里developer.android.com/reference/android/view/…

标签: android android-widget


【解决方案1】:

这是可能的。你必须这样做:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); // your widget layout xml
for (...){
    RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.empty_item); // xml with just a textview
    view.setTextViewText(R.id.textviewId, "some dynamic text"); // id of the textview and some text
    views.addView(R.id.layout, view); // id of the layout in widget.xml    
}
appWidgetManager.updateAppWidget(appWidgetId, views);

【讨论】:

    【解决方案2】:

    不,这是不可能的。

    但是,您可以使用一个多行 TextView。

    【讨论】:

      【解决方案3】:
        TextView[] tx = new TextView[10];
          TableRow[] tr = new TableRow[10];
          for(i=0; i<10; i++) {
              tx[i] = new TextView(this);
              tx[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
              tx[i].setText("Data")
              tr[i] = new TableRow(this);
              tr[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
              tr[i].addView(tx[i]);
              // and then adding table row in tablelayout
          }
      

      【讨论】:

      • 正如我在上面的 cmets 中所说,这不适用于小部件!
      • 你对数组大小有限制吗?
      • 理论上是的,它有一个限制,但它是用户定义的,所以它可以在上限方面变化很大!
      【解决方案4】:

      如果你把它放在你的 onCreate 中,这样的东西应该可以工作:

      View myView  = (View)findViewById(R.id.Something);
      
      for(element : array) {
      
          TextView myTV = new TextView(context);
          myTV.setText(array.getText());
          myView.addView(myTV);
      
      }
      

      【讨论】:

      • 不,这对小部件不起作用!我正在使用 AppWidgetProvider 扩展类而不是 Activity!
      • 代替 findViewByID .. 你使用 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.mywidget);
      • 我知道,但是像这样在运行时添加 TextViews 是行不通的!它必须是预定义的 xml,我在我的问题上说过!
      • 那么为什么你不能使用上面的方法并使用 AppWidgetManager 更新你的小部件
      • 因为正如我所说,这不适用于小部件!我已经尝试过,也尝试过类似的解决方法,但没有任何运气!
      猜你喜欢
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多