在Android中将视图添加到RemoteViews

时间:2013-01-12 05:56:54

标签: android widget

直接解决问题。

//This is my main layout
RemoteViews views = null;
views = new RemoteViews(getPackageName(), R.layout.main); 

//Clear parent view
views.removeAllViews(R.id.llMain);

//This is how we add nested view
RemoteViews childView = new RemoteViews(getPackageName(), R.layout.lay_child);
views.addView(R.id.llMain, childView);

代码在Galaxy Nexus上运行良好。但不是在Galaxy Tab或Motorola等其他设备上。有什么问题?

它会返回类似“无法找到视图”的错误消息。请改用错误视图。 updateAppWidget使用错误视图找不到任何视图 android.widget.RemoteViews $ ActionException:找不到视图:0x7f050025

//This is the main layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#00000000"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/llMain"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"    
        android:orientation="vertical"
        android:layout_gravity="center|left"
        android:layout_marginLeft="@dimen/marginLeft">  

    </LinearLayout>             

</FrameLayout>

这是儿童观点

lay_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llTimezoned"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="visible"
    android:layout_gravity="center">                    
    <ImageView android:id="@+id/ivTimezone"
        android:layout_width="60dp"
        android:layout_height="60dp"
        />     
</LinearLayout>

或者它与操作系统版本有关吗?

1 个答案:

答案 0 :(得分:1)

这对我有用

RemoteViews update = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    for (int i = 0; i < 3; i++) {
        RemoteViews textView = new RemoteViews(context.getPackageName(), R.layout.test);
        textView.setTextViewText(R.id.textView1, "TextView number " + String.valueOf(i));
        update.addView(R.id.ll_widget, textView);
    }

    appWidgetManager.updateAppWidget(appWidgetIds, update);

https://stackoverflow.com/a/20040066/1492815