简体   繁体   English

多次包含布局时在布局内设置Android RemoteViews的文本

[英]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.

相关问题 以编程方式创建布局时,Android“无法为...扩展remoteViews” - Android 'Couldn't expand remoteViews for…' when creating layout programmatically android-xml-多次包含相同布局不起作用 - android - xml - include same layout multiple times does not work 使用RemoteViews的Android自定义通知布局 - Android custom notification layout with RemoteViews Android RemoteViews,如何在窗口小部件中设置ImageView的ScaleType? - Android RemoteViews, how to set ScaleType of an ImageView inside a widget? 多次包含相同的布局 - Include same layout multiple times 在布局中的TextView中动态设置文本 - Android - Dynamically set text in a TextView inside a Layout - Android 带有 RemoteViews 的 Android 通知 - 具有与 RemoteViews 布局相关联的活动 - Android notification with RemoteViews - having activity associated with RemoteViews layout 安卓系统 - Android <include layout inside NestedScrollView 为什么Android应用程序小部件的布局基于RemoteViews? - Why the layout of Android app widgets is based on RemoteViews? 带有 RemoteViews 的 Android 5+ 自定义通知 XML 布局,为 ImageButton 设置正确的图标色调 - Android 5+ custom notification XML layout with RemoteViews, set correct icon tint for ImageButton
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM