我已经搜索了ImageView解决方案,但我无法执行一些代码,因为它可能是通过Visual Studio在Android Studio中完成的。
我想在Main.axml中显示存储在位置(Android资源/文件系统)的图像,以下是Main的XML: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RelativeLay"
tools:context=".MainActivity">
<VideoView
android:id="@+id/zoneVid"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/zoneImg" />
</RelativeLayout>
图像当前存在于“drawable”文件夹中,序列命名为“view01” - “view06”。 OnCreate方法,我将嵌入文件读入动态对象并拆分成数组,因为每一行都包含每个图像名称,宽度,高度,顶部和左边距。
以下是MainActivity.cs文件的完整代码: -
public class MainActivity : Activity//, MediaPlayer.IOnPreparedListener, ISurfaceHolderCallback
{
public static string PACKAGENAME;
ImageView zoneImg;
MediaPlayer zonePlayer;
VideoView zoneVid;
RelativeLayout rl;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
// GET PACKAGE NAME FROM APPLICATION
MainActivity.PACKAGENAME = ApplicationInfo.PackageName;
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager.DefaultDisplay.GetRealMetrics(displayMetrics);
int height = displayMetrics.HeightPixels;
int width = displayMetrics.WidthPixels;
Assembly assem = this.GetType().Assembly;
var content = (dynamic)null;
using (System.IO.Stream stream = assem.GetManifestResourceStream(assem.GetName().Name + '.' + "SampleZone.txt"))
{
if (stream != null)
{
using (var reader = new StreamReader(stream))
{
content = reader.ReadToEnd();
}
}
}
if (content != null)
{
rl = FindViewById<RelativeLayout>(Resource.Id.RelativeLay);
RelativeLayout.LayoutParams param;
int resourceId;
string[] zone_element = content.Split(new[] { "\r\n", "\r", "\n" }, System.StringSplitOptions.None);
var gridView = new GridLayout(this);
foreach (var zone_line in zone_element)
{
string[] zone_details = zone_line.Split('|');
double widthpercent = Convert.ToDouble(zone_details[1]), heightpercent = Convert.ToDouble(zone_details[2]);
String FileName = zone_details[5].ToString();
String FileExtension = FileName.Substring(FileName.LastIndexOf("."));
switch (FileExtension)
{
case ".png":
case ".bmp":
case ".gif":
case ".webp":
case ".jpeg":
case ".jpg":
{
zoneImg = new ImageView(this);
int virt_width = (int)Math.Round(width * (widthpercent / 100));
int virt_height = (int)Math.Round(height * (heightpercent / 100));
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(virt_width, virt_height);
layout.SetMargins(Convert.ToInt32(zone_line[3]), Convert.ToInt32(zone_line[4]), 0, 0);
resourceId = (int)typeof(Resource.Drawable).GetField(System.IO.Path.GetFileNameWithoutExtension(FileName)).GetValue(null);
zoneImg.SetImageResource(resourceId);
zoneImg.RequestLayout();
//param = new RelativeLayout.LayoutParams(virt_width, virt_height)
//{
// LeftMargin = Convert.ToInt32(zone_line[3]),
// TopMargin = Convert.ToInt32(zone_line[4])
//};
//zoneImg.LayoutParameters= new LayoutParams(param);
//rl.AddView(zoneImg);
break;
}
case ".avi":
case ".3gp":
case ".webm":
case ".mkv":
case ".mp4":
{
//zoneVid = FindViewById<VideoView>(Resource.Id.zoneVid);
//resourceId = (int)typeof(Resource.Drawable).GetField(System.IO.Path.GetFileNameWithoutExtension(FileName)).GetValue(null);
//var uri = Android.Net.Uri.Parse("android.resource://" + MainActivity.PACKAGENAME + '/' + resourceId);
//zoneVid.SetVideoURI(uri);
//zoneVid.SetMediaController(null);
//int virt_width = (int)Math.Round(width * (widthpercent / 100));
//int virt_height = (int)Math.Round(height * (heightpercent / 100));
//RelativeLayout.LayoutParams rltvparams = new RelativeLayout.LayoutParams(virt_width, virt_height)
//{
// LeftMargin = Convert.ToInt32(zone_line[3]),
// TopMargin = Convert.ToInt32(zone_line[4])
//};
//zoneVid.LayoutParameters = new LayoutParams(rltvparams);
////rl.AddView(zoneVid);
//zoneVid.RequestFocus();
//zoneVid.Visibility = ViewStates.Visible;
//zoneVid.Start();
break;
}
}
}
}
}
}
我已经对我的代码发表了评论,因为我尝试了几件事,因为图像在边距和尺寸上没有正确。我看不到Visual Studio Intellisense功能出现在
上image.getLayoutParams()。height = 100;
imageView.setLayoutParams(PARAMS);
我从一些解决方案/讨论/教程中发现但无法弄清楚: -
答案 0 :(得分:0)
你可以这样使用。
var param = (RelativeLayout.LayoutParams)zoneImg.LayoutParameters;
params.SetMargins (0,0,0,10);
//Or
params.BottomMargin = 10;
myView.LayoutParameters = params;