1

ich auf diese Frage beziehen: How do I access the views inside the layout when I reuse it multiple times?Set Text von Android RemoteViews innerhalb Layout, wenn enthalten sie mehrere Male

Ich habe die beiden folgenden 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> 

und

<!-- 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> 

Normalerweise , können Sie auf die TextViews programmatisch innerhalb des enthaltenen Layouts wie folgt zugreifen:

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"); 

Aber in meinem Fall habe ich ein Android AppWidget, die über RemoteViews nur zugegriffen werden:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); 
views.setTextViewText(R.id.text_view, "test1"); 

Dies nur den Text des ersten Textview ändern.

Ich habe keine Lösung gefunden, also meine Frage ist, ob es möglich ist, den Text des TextView innerhalb mehrerer enthaltener Layouts zu setzen.

Antwort

0

Kann gemacht werden.

Entfernen Sie Ihre include s; Sie benötigen programmatisch (keine andere Möglichkeit, AFAIK), so fügen ID (z LinearLayout), um Ihre Widgets Linearlayout hinzuzufügen, und:

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); 
Verwandte Themen