Xamarin Android 小部件:如何以编程方式更改 ImageView 的高度?

Xamarin Android Widget: How to change Height of ImageView programmatical?

我有一个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" />

如何更改图片的大小?

[Service]
public class UpdateService : Service
{
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        var updateViews = new RemoteViews(context.PackageName, Resource.Layout.widget);
        // changing textsize works perfectly:
        updateViews.SetTextViewTextSize(Resource.Id.mytext, (int)ComplexUnitType.Sp, 14);
        updateViews.SetViewVisibility(Resource.Id.myimage, ViewStates.Gone);
        updateViews.SetTextViewText(Resource.Id.text, "new text");

        // but changing imagesize does not work, I didn't find a right way to set the size, need something like this:
        updateViews.SetInt(Resource.Id.myimage, "layout_width ???", 200dip ???);

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


    }

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;

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

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

    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,并将Visibility设置为Gone。通过调用 SetViewVisibility().

显示代码中要显示的那个