【发布时间】: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