Xamarin c# 无法让图像在 ScrollView 中的 LinearLayout 中工作
Xamarin c# Cant get a image to work in a LinearLayout which is in a ScrollView
我在 xamarin 中遇到了这个问题,我在其中制作了一个名为 picview 的自定义视图,它只是制作了一个位图并绘制了它,而我的 activity class DatLocPage 我想将此图片放在 LinearLayout 中它在 Scrollview 中,但它似乎不起作用。当我只对 picview 使用 SetContentView 时它确实有效。
我的 class DatLocPage 代码是:
[Activity(标签 = "")]
class 数据库页面:Activity
{
受保护的覆盖 void OnCreate(Bundle b)
{
base.OnCreate(b);
ScrollView upmenu = new ScrollView(this);
upmenu.SetBackgroundColor(Color.White);
LinearLayout menu = new LinearLayout(this);
menu.Orientation = Orientation.Vertical;
menu.SetBackgroundColor(Color.White);
upmenu.AddView(menu);
picview pic = new picview(this);
menu.AddView(pic);
Button Back = new Button(this); Back.Text = "Back"; Back.Click +=
klikback; menu.AddView(Back);
this.SetContentView(upmenu);
}
对于我的自定义视图 picview,它是:
class picview : View
{
Bitmap Plaatje;
public picview(Context c) : base(c)
{
this.SetBackgroundColor(Color.White);
Plaatje = BitmapFactory.DecodeResource(c.Resources, Resource.Drawable.watch);
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
Paint verf = new Paint();
canvas.DrawBitmap(Plaatje, 0, 0, verf);
}
}
您在视图中遗漏了一堆 LayoutParameters
,它们告诉它们应该有多大。
看看这个修改后的代码:
ScrollView upmenu = new ScrollView(this);
upmenu.SetBackgroundColor(Color.White);
upmenu.LayoutParameters =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent);
LinearLayout menu = new LinearLayout(this);
menu.Orientation = Orientation.Vertical;
menu.SetBackgroundColor(Color.White);
menu.LayoutParameters =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent);
upmenu.AddView(menu);
var pic = new picview(this);
pic.LayoutParameters =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
200);
menu.AddView(pic);
Button Back = new Button(this);
Back.Text = "Back";
Back.LayoutParameters =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
50);
Back.Click += klikback;
menu.AddView(Back);
SetContentView(upmenu);
请注意,200
和 50
以像素为单位,您应该考虑使用 dp
。有大量 post 解释了如何转换这些值。
您的 picview
中的 Bitmap
也没有被处理掉,您可能会泄漏内存。所以要小心。
我在 xamarin 中遇到了这个问题,我在其中制作了一个名为 picview 的自定义视图,它只是制作了一个位图并绘制了它,而我的 activity class DatLocPage 我想将此图片放在 LinearLayout 中它在 Scrollview 中,但它似乎不起作用。当我只对 picview 使用 SetContentView 时它确实有效。
我的 class DatLocPage 代码是:
[Activity(标签 = "")] class 数据库页面:Activity { 受保护的覆盖 void OnCreate(Bundle b) { base.OnCreate(b);
ScrollView upmenu = new ScrollView(this);
upmenu.SetBackgroundColor(Color.White);
LinearLayout menu = new LinearLayout(this);
menu.Orientation = Orientation.Vertical;
menu.SetBackgroundColor(Color.White);
upmenu.AddView(menu);
picview pic = new picview(this);
menu.AddView(pic);
Button Back = new Button(this); Back.Text = "Back"; Back.Click +=
klikback; menu.AddView(Back);
this.SetContentView(upmenu);
}
对于我的自定义视图 picview,它是:
class picview : View
{
Bitmap Plaatje;
public picview(Context c) : base(c)
{
this.SetBackgroundColor(Color.White);
Plaatje = BitmapFactory.DecodeResource(c.Resources, Resource.Drawable.watch);
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
Paint verf = new Paint();
canvas.DrawBitmap(Plaatje, 0, 0, verf);
}
}
您在视图中遗漏了一堆 LayoutParameters
,它们告诉它们应该有多大。
看看这个修改后的代码:
ScrollView upmenu = new ScrollView(this);
upmenu.SetBackgroundColor(Color.White);
upmenu.LayoutParameters =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent);
LinearLayout menu = new LinearLayout(this);
menu.Orientation = Orientation.Vertical;
menu.SetBackgroundColor(Color.White);
menu.LayoutParameters =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.MatchParent);
upmenu.AddView(menu);
var pic = new picview(this);
pic.LayoutParameters =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
200);
menu.AddView(pic);
Button Back = new Button(this);
Back.Text = "Back";
Back.LayoutParameters =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MatchParent,
50);
Back.Click += klikback;
menu.AddView(Back);
SetContentView(upmenu);
请注意,200
和 50
以像素为单位,您应该考虑使用 dp
。有大量 post 解释了如何转换这些值。
您的 picview
中的 Bitmap
也没有被处理掉,您可能会泄漏内存。所以要小心。