Xamarin.android XamarinAndroid小部件:如何更改ImageView程序的高度?

Xamarin.android XamarinAndroid小部件:如何更改ImageView程序的高度?,xamarin.android,android-widget,Xamarin.android,Android Widget,我有一个widget.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusa

我有一个widget.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
style="@style/WidgetBackground">
<ImageView
    android:id="@+id/myimage"
    android:layout_width="20dip"
    android:layout_height="20dip"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/myimage" />
<TextView
    android:id="@+id/mytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"       
    style="@style/WidgetText.Title" />

updateViews有几个setInt、setFloat、Set。。但是我没有找到正确的方法。有什么提示吗?

您可以尝试以下方法:

var videoContainer = FindViewById<RelativeLayout>(Resource.Id.local_video_container);
var parameters = videoContainer.LayoutParameters;
parameters.Height = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, containerHeight, Resources.DisplayMetrics);
parameters.Width = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, containerWidth, Resources.DisplayMetrics);
videoContainer.LayoutParameters = parameters;
var videoContainer=findviewbyd(Resource.Id.local\u video\u container);
var参数=videoContainer.LayoutParameters;
parameters.Height=(int)TypedValue.ApplyDimension(ComplexUnitType.Dip、containerHeight、Resources.DisplayMetrics);
parameters.Width=(int)TypedValue.ApplyDimension(ComplexUnitType.Dip、containerWidth、Resources.DisplayMetrics);
videoContainer.LayoutParameters=参数;

无法在代码中更改
图像视图的高度或宽度,但有一些解决方法

您可以尝试创建一个新的小部件布局,使
ImageView
的大小符合您的要求。调用
remoteview()
时,请使用它,而不是原始的。当您更新小部件时,它将被重新绘制。 例如:

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        //var updateViews = new RemoteViews(this.PackageName, Resource.Layout.widget);
        //Using another layout instead.
        var updateViews = new RemoteViews(this.PackageName, Resource.Layout.layout1);

        ComponentName thisWidget = new ComponentName(this, Java.Lang.Class.FromType(typeof(MyWidget)).Name);
        AppWidgetManager manager = AppWidgetManager.GetInstance(this);
        manager.UpdateAppWidget(thisWidget, updateViews);
        ///...
        ///...

    }

或者您也可以尝试在布局中添加许多不同大小的
ImageView
,并将
可见性设置为
Gone
。通过调用
SetViewVisibility()

来显示要在代码中显示的内容,谢谢,但是FindViewById在小部件上下文中似乎不可用。您能更新完整的xml并澄清什么是updateViews吗
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        //var updateViews = new RemoteViews(this.PackageName, Resource.Layout.widget);
        //Using another layout instead.
        var updateViews = new RemoteViews(this.PackageName, Resource.Layout.layout1);

        ComponentName thisWidget = new ComponentName(this, Java.Lang.Class.FromType(typeof(MyWidget)).Name);
        AppWidgetManager manager = AppWidgetManager.GetInstance(this);
        manager.UpdateAppWidget(thisWidget, updateViews);
        ///...
        ///...

    }