事实证明,第一个链接中的答案通常是正确的,但我无法完全按照发布的方式运行它,因此我需要更多详细信息才能让它在我的项目中运行。我在下面发布了我的代码以供将来参考。
请注意,在其他线程中没有指定的一个非常重要的地方是rv.addView() 需要与rv 对象中指定的相同布局;也就是说,我不能将LinearLayout 嵌套在另一个LinearLayout 中,然后引用内部LinearLayout。我必须引用外部的,因为这是父 rv 实例中引用的。
我的onUpdate() 专用于 Samsung Look 的 Edge Panel API,但一般格式相同:
@Override
public void onUpdate(Context context, SlookCocktailManager manager, int[] cocktailIds) {
//Parent RemoteViews object
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main_view);
for (int i = 0; i < someObjectArray.length; i++) {
//Create new remote view using the xml file that represents a list entry
RemoteViews listEntryLayout = new RemoteViews(context.getPackageName(), R.layout.list_entry);
//Set the text of the TextView that is inside the above specified listEntryLayout RemoteViews
listEntryLayout.setTextViewText(R.id.stock_text, someObjectArray[i].getName());
//Add the new remote view to the parent/containing Layout object
rv.addView(R.id.main_layout, listEntryLayout);
}
//standard update
if (cocktailIds != null) {
for (int id : cocktailIds) {
manager.updateCocktail(id, rv);
}
}
}
main_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="2"
android:orientation="vertical" >
</LinearLayout>
list_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/stock_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/stock_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/holo_green_light"
android:text="TextView"
android:textSize="20dp"/>
</LinearLayout>