改变编程控件布局 [英] Changing Widget Layout Programmatically

查看:154
本文介绍了改变编程控件布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我有两个布局的窗口小部件:布局1和布局2。 默认为小部件是布局1,但我让用户选择 其布局他们想要的部件是。因此,如果用户改变到 布局2,我怎么编程的布局更改为布局2?

Let's say that I have two layouts for a widget: Layout1 and Layout2. The default for the widget is Layout1, but I allow the user to choose which layout they want the widget to be. So if the user changes to Layout2, how do I programmatically change the layout to Layout2?

没有一个的setContentView方法部件像有对活动。

There isn't a setContentView method for widgets like there is for Activities.

感谢

推荐答案

您必须选择的布局,当你建立你的远程视窗。在我的小部件code:

You have to choose the layout when you're building your remoteView. In my widget code:

public static RemoteViews buildUpdate(Context context, String action) {
    RemoteViews updateViews;    		
    SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
    String typeface = prefs.getString("typeface", "sans");
    int layoutId = R.layout.widget_sans;
    if ("monospace".equals(typeface)){
        layoutId = R.layout.widget_mono;
    } else if ("serif".equals(typeface)){
        layoutId = R.layout.widget_serif;
    }
    updateViews = new RemoteViews(context.getPackageName(),
        layoutId);
    //actually do things here
    //then finally, return our remoteView
    AppWidgetManager.getInstance(context).updateAppWidget(
        new ComponentName(context, FuzzyWidget.class), updateViews);

}

这篇关于改变编程控件布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆