android - android Widget中的动态布局

标签 android dynamic widget scrollview appwidgetprovider

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

我想像这样在行中绘制图像: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);
    }
}

最佳答案

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

android:id="@+id/imageScrollLayout"

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

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

要动态添加新的 ImageViews 到布局,使用

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 文档非常适合查找所有相关方法。此外,如果您在 Activity 的 onCreate 方法以外的任何地方添加图像,请记住将 imageScrollLayout 设为 Activity 中的一个字段,而不是 onCreate 方法中的本地字段,这样您就可以在任何地方访问它。

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

将它们附加到小部件
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,因此很难实现您想要的水平滚动。这可能是 Google 的设计决定,因为 AppWidget 中的水平滚动与主屏幕之间的水平滚动效果不佳。也许改用左右边缘按钮来显示新的图像页面?

关于android - android Widget中的动态布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12159650/

相关文章:

xml - Inputmask 小部件不适用于 odoo 10 c

java - Bitmap.CreateBitmap 在 JUnit 测试 Android Studio 中返回 null

c++ - 我的 append 功能没有按预期工作。 C++

javascript - Jquery Animate 高度切换

html - 保护 iFrame - 只允许它在一个域上工作

c++ - C++ 中没有指针的动态内存分配——指针有什么意义?

android - Timber 库到底是做什么的?

Android 媒体播放器循环无间隙(歌曲开始和结束时无静音)

android - 浏览器图像显示不正确

Javascript 函数递归(又名 'do more with less' )