[英]Set text of Android RemoteViews inside layout when include it multiple times
I refer to this question: How do I access the views inside the layout when I reuse it multiple times? 我指的是这个问题: 多次重复使用布局时,如何访问视图?
I have the following two layouts: 我有以下两种布局:
<!-- layout_to_include.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
and 和
<!-- widget.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/included_layout_1"
layout="@layout/layout_to_include"/>
<include
android:id="@+id/included_layout_2"
layout="@layout/layout_to_include"/>
</LinearLayout>
Normally, you can access the TextViews programmatically inside the included layout like this: 通常,您可以像这样在包含的布局内以编程方式访问TextViews:
LinearLayout l1 = (LinearLayout) findViewById(R.id.included_layout_1);
((TextView) l1.findViewById(R.id.text_view)).setText("test1");
LinearLayout l2 = (LinearLayout) findViewById(R.id.included_layout_2);
((TextView) l2.findViewById(R.id.text_view)).setText("test2");
But in my case, I have an Android AppWidget which can only accessed via RemoteViews: 但就我而言,我有一个Android AppWidget,只能通过RemoteViews访问:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.text_view, "test1");
This will only change the text of the first TextView. 这只会更改第一个TextView的文本。
I have not find any solution, so my question is whether is it possible to set the text of the TextView within multiple included layouts. 我没有找到任何解决方案,所以我的问题是是否可以在多个包含的布局中设置TextView的文本。
Can be done. 可以做到。
Remove your include
s; 删除您的include
; you need to add programmatically (no other way AFAIK), so add id (eg LinearLayout
) to your widget's linearlayout, and: 您需要以编程方式添加(无需其他方式AFAIK),因此将id(例如LinearLayout
)添加到小部件的linearlayout中,并:
RemoteViews one = new RemoteViews(getPackageName(), R.layout.layout_to_include);
one.setTextViewText(R.id.text_view, "tv-ONE"); // <---
remoteViews.addView(R.id.LinearLayout, one);
RemoteViews two = new RemoteViews(getPackageName(), R.layout.layout_to_include);
two.setTextViewText(R.id.text_view, "tv-TWO"); // <---
remoteViews.addView(R.id.LinearLayout, two);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.