RemoteView addView not working

前端 未结 1 1685
小蘑菇
小蘑菇 2020-12-17 19:40

I have an app widget and I\'d like to add Views (TextView, etc.,) to the RemoteView but it never shows up.
Here goes the code:



        
相关标签:
1条回答
  • 2020-12-17 20:21

    The addView() method needs the id of the view inside the layout you want to add this new view to, not the layout itself.

    Instead of this:

    views.addView(views.getLayoutId(), newView);
    

    Try this:

    views.addView(R.id.view_container, newView);
    

    Assuming your layout looks something like this:

    file: layout/widget_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/view_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <!-- New views will be added here at runtime -->
        </LinearLayout>
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题