【问题标题】:Dynamic layout in android Widgetandroid Widget中的动态布局
【发布时间】:2013-05-22 05:32:22
【问题描述】:

是否可以在 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);
    }
}

【问题讨论】:

    标签: android dynamic widget scrollview appwidgetprovider


    【解决方案1】:

    在你的xml布局中创建一个Horizo​​ntalScrollView,在里面放一个水平LinearLayout,并通过设置id属性给layout一个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 文档非常适合查找所有相关方法。此外,如果您在 Activity 的 onCreate 方法之外的任何位置添加图像,只需记住将 imageScrollLayout 设为 Activity 中的字段,而不是 onCreate 方法中的本地字段,这样您就可以在任何地方访问它。

    编辑:要在 AppWidgetProvider 中实现这一点,您需要重写 onUpdate 方法。每当系统请求更新小部件时,都会调用此方法。如果确实需要更改小部件的 RemoteViews,则需要创建一组新视图,并使用

    将它们附加到小部件
    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);
    

    找到您的布局并将 ImageViews 添加到其中。

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

    【讨论】:

    • 是的,我知道如何在活动类中创建动态布局,但我不知道如何在 AppWidgetProvider 中创建。您的示例是针对正常活动的。可以在小部件中使用吗?
    • 可能想要编辑问题 - 所有可以拖放到活动布局中的标准视图都称为小部件。但是,这在实现 AppWidgetProvider 时是类似的,请参阅编辑后的答案。
    • 到目前为止,我用我的代码更新了帖子。我现在将尝试实施您的答案! brb :)
    • 我不确定如何理解这个答案。你在说什么Activity?以及如何访问您传递给 WidgetProvider 的布局的 root 布局(您说的是 Widget 对吗?)您能否在此处查看我的问题 stackoverflow.com/questions/22582260/…,看看您是否可以调整您的答案要回答吗?我怀疑它也会澄清 *this answer 中对我没有意义的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多