向FrameLayout添加视图 - Xamarin,Android,Monodroid

时间:2014-09-17 13:10:32

标签: android xamarin xamarin.android

如何向FrameLayout添加视图。我希望能够从自定义View类创建一个新视图,并将其添加到FrameLayout。自定义视图将具有将与特定可绘制/位图关联的OnDraw()方法。

我希望能够在图片上绘制图片,但我似乎无法使我的布局/视图正常工作。

我是否正确地将ImageView添加到我的FrameLayout?

protected override void OnCreate (Bundle savedInstanceState)
    {
        base.OnCreate (savedInstanceState);
        this.image = new ImageView (this);
        //image = FindViewById<ImageView> (Resource.Id.imageView1);
        //this.image.SetScaleType (ImageView.ScaleType.CenterInside);
        //SetContentView (this.image, new ViewGroup.LayoutParams (ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent));
        FrameLayout m_Layout = FindViewById<FrameLayout>(Resource.Id.frameLayout1);
        //m_Layout.AddView (image);
        SetContentView (m_Layout);

        this.path = (savedInstanceState ?? Intent.Extras).GetString ("path");
        Title = System.IO.Path.GetFileName (this.path);

        Cleanup();
        DecodeBitmapAsync (path, 400, 400).ContinueWith (t => {
            image = FindViewById<ImageView>(Resource.Id.imageView1);
            image.SetImageBitmap (this.bitmap = t.Result);
        }, TaskScheduler.FromCurrentSynchronizationContext());
    }

XML

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="match_parent"
    p1:layout_height="match_parent"
    p1:id="@+id/frameLayout1">
    <ImageView
        p1:src="@android:drawable/ic_menu_gallery"
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/imageView1" />
</FrameLayout>

1 个答案:

答案 0 :(得分:0)

您没有将图片添加到FrameLayout。您已注释掉已关闭的行,但您需要在您创建的新LayoutParameters上设置ImageView。在你致电SetContentView(m_Layout)之后,就这样了。

this.image = new ImageView(this);
this.image.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
m_Layout.AddView(this.image);

现在,您应该可以设置this.image位图并将其显示在屏幕上。