android Widget中的动态布局

时间:2012-08-28 12:52:13

标签: android dynamic widget scrollview appwidgetprovider

是否可以在AppWidgetProvider中创建动态布局?我只找到了使用XML创建小部件的教程,但不可能以这种方式创建视图..

我想在这样的行中绘制图像:http://imgur.com/7j18q 并添加水平滚动。有可能吗?

问候!

编辑:(这是我到目前为止......但我仍然不知道如何添加这些图片)

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    final int N = appWidgetIds.length;
    for(int i = 0; i<N; i++){
        int awID = appWidgetIds[i];
        RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.about);
        try
        {
            j = 1;
            Set<User> users = LFMRestConnection.getFriendsNowListening(user, 1440);;
            for (User u : users) {
                album_pic = loadBitmap(u.getImageURL().getImageURL(ImageURL.Size.LARGE));
                resized_a_p = android.graphics.Bitmap.createScaledBitmap(album_pic, (50), (50), true);
            //URLS
                url_to_enter[j]=u.getLastTrack().getArtistName();
                track_to_enter[j]=u.getLastTrack().getTrackName();
            //profile picture
                user_pic = loadBitmap(u.getImageURL().getImageURL(ImageURL.Size.LARGE));
                resized_u_p = android.graphics.Bitmap.createScaledBitmap(user_pic, (25), (25), true);
                j++; 
            }
        }
        catch(Exception e)
        {
            //startActivity(i);
            System.out.println("widget error");     
        }
        appWidgetManager.updateAppWidget(awID, v);
    }
}

1 个答案:

答案 0 :(得分:2)

在xml布局中创建Horizo​​ntalScrollView,在其中放置水平LinearLayout,并通过设置id属性为 布局 提供ID

android:id="@+id/imageScrollLayout"

然后,在您的活动的onCreate中,找到布局对象

LinearLayout imageScrollLayout = (LinearLayout)findViewById(R.id.imageScrollLayout); 

要动态地将新ImageView添加到布局,请使用

ImageView newImageView=new ImageView();

//set the ImageView image with one of the following:
newImageView.setImageBitmap(bm);
newImageView.setImageDrawable(drawable);
newImageView.setImageResource(resId);

//add the new ImageView to your layout
imageScrollLayout.addView(newImageView);

可以在代码中访问ImageView的所有xml设置,只需在设置图像后进行设置即可。 Google Android文档非常适合查找所有相关方法。此外,如果您要为活动添加图像而不是onCreate方法,请记住将imageScrollLayout设置为活动中的字段,而不是onCreate方法中的本地字段,以便您可以随处访问它。

编辑:要在AppWidgetProvider中实现此功能,您需要覆盖onUpdate方法。只要系统请求窗口小部件的更新,就会调用此方法。如果确实需要更改窗口小部件的RemoteView,则需要创建一组新视图,并使用

将它们附加到窗口小部件
appWidgetManager.updateAppWidget(componentName, remoteViews)

其中appWidgetManager是onUpdate方法的提供参数。需要在onAppWidgetOptionsChanged()方法中实现类似的过程。要提供remoteViews参数,请使用符合

效果的内容在代码中膨胀布局文件
LayoutInflater myInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
final View myRootLayout = myInflater .inflate(R.layout.myLayout, null);

之后你可以使用正常的

myRootLayout.findViewById(R.id.myView);

找到您的布局并向其添加ImageView。

毕竟,我怀疑Horizo​​ntalScrollView不支持RemoteView,这使得很难实现你想要的水平滚动。这可能是谷歌的设计决定,因为AppWidget中的水平滚动与主屏幕之间的水平滚动效果不佳。也许使用左右边缘按钮来显示新的图像页面?