首页 > 解决方案 > 如何更改 Xamarin RemoteView 中的 TextBox 背景颜色

问题描述

我正在尝试在我的 Xamarin.Forms 应用程序中为 Android 创建一个小部件。我正在尝试练习的示例在这里:AppWidgetListView。问题是,我找不到改变 TextBox 背景颜色的方法。适用于 Java的remoteView.setInt(R.id.container, "SetBackgroundColor", backgroundColor);方法在 Xamarin 中不起作用。此外,它甚至不会更改可绘制图片。拜托,你能帮帮我吗?

标签: androidxamarinwidget

解决方案


您可以直接在布局中进行list_row.xml,只需在根布局上设置背景

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@android:color/holo_green_dark" >  //this line 

如果你想在代码中改变它


//ListProvider.cs
public RemoteViews GetViewAt(int position)
{
     RemoteViews remoteView = new RemoteViews( context.PackageName, Resource.Layout.list_row);

     //add this line
     remoteView.SetInt(Resource.Id.container, "setBackgroundColor", Android.Graphics.Color.Red);

     ...
}

在此处输入图像描述


推荐阅读