【问题标题】:How to use activity_main layout as a widget on xamarin?如何在 xamarin 上使用 activity_main 布局作为小部件?
【发布时间】:2019-12-24 14:34:25
【问题描述】:

我有一个 xamarin 项目,我的主要目的是使用 activity_main 布局创建一个小部件,我的活动主布局包括选项卡式页面,例如 Tab1 、 Tab2 和 Tab3 。这就是为什么我想在小部件上显示它们。根据我的研究,我需要一些 xml 文件来在 android 上创建一个小部件。我的xml文件是这样的。

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dip"
    android:minHeight="72dip"
    android:resizeMode="horizontal"
      android:minResizeWidth="144dip"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/activity_main"/>

我添加了 activity_main 作为初始布局,并在包中做了一些安排。我的 appwidget 类是这样的。

    [BroadcastReceiver(Label = "Widget")]
    [IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE" })]
    [MetaData("android.appwidget.provider", Resource = "@xml/widget_word")]
    class AppWidget : AppWidgetProvider
    {
        public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
        {             
            context.StartService(new Intent(context, typeof(UpdateService)));
        }
    }

我的 updateService 是这样的。

 public override void OnStart(Intent intent, int startId)
        {
            // Build the widget update for today
            RemoteViews updateViews = buildUpdate(this);

            // Push update for this widget to the home screen
            ComponentName thisWidget = new ComponentName(this, Java.Lang.Class.FromType(typeof(AppWidget)).Name);
            AppWidgetManager manager = AppWidgetManager.GetInstance(this);
            manager.UpdateAppWidget(thisWidget, updateViews);
        }




        public override IBinder OnBind(Intent intent)
        {
            throw new NotImplementedException();
        }


        public RemoteViews buildUpdate(Context context)
        {
            // Build an update that holds the updated widget contents
            var updateViews = new RemoteViews(context.PackageName, Resource.Layout.activity_main);

            return updateViews;
        }

我正在使用片段来显示 tab1 、 tab2 、 tab3 。如果我将片段 axml 文件添加到 initialLayout 它工作正常。但它只显示 tab1 。我觉得我应该使用 activity_main 在小部件上显示 3 个选项卡。请帮忙!

【问题讨论】:

  • 不幸的是,我的问题不是直接将 activity_main 布局添加到小部件中。
  • widget direct 是什么意思?
  • 你不能使用bottomNavigationView,如果你想显示一个小部件,你只允许使用远程布局(FrameLayout,LinearLayout,RelativeLayout,GridLayout)和以下小部件类(AnalogClock,Button,Chronometer,ImageButton,ImageView,ProgressBar,TextView,ViewFlipper,@9876543@38@,@9876544339@,@9876544339@,@9865 , 我把bottomNavigationView 放在LinearLayout 中,放到桌面还是不能显示小部件。
  • 不客气,我可以发表评论回答吗?它将帮助其他有类似问题的人。

标签: xamarin xamarin.android widget


【解决方案1】:

你不能在appwidget中使用bottomNavigationView

如果你设置它,你会得到跟随错误。

如果您熟悉布局,则创建 App Widget 布局很简单。但是,您必须注意,App Widget 布局基于RemoteViews,并不支持所有类型的布局或视图小部件。

RemoteViews 对象(以及相应的 App Widget)可以支持以下布局类:

FrameLayout LinearLayout RelativeLayout GridLayout

还有以下小部件类:

AnalogClock Button Chronometer ImageButton ImageView ProgressBar TextView ViewFlipper ListView GridView StackView AdapterViewFlipper

不支持这些类的后代。

这是关于它的 Google 文章。 https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 2022-01-10
    • 2021-01-12
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多