我有一个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 ..但是我找不到合适的方法。有什么提示吗?
答案 0 :(得分:0)
您可以尝试以下方法:
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;
答案 1 :(得分:0)
在代码中更改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()
显示您要在代码中显示的那个。