Android Xamarin

时间:2016-04-14 08:55:34

标签: c# android visual-studio xamarin.android

我正在与Xamarin和Visual studio合作。我想在我的应用程序上从REST服务上动态添加ImageView。

我的Main.axml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout1"
    android:minWidth="25px"
    android:minHeight="25px">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearOnlineOffers" />
    </ScrollView>
</LinearLayout>

我的MainActivity.cs是这样的:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        string url = "http://192.168.90.102/test_api/handler.php/";

        Online[] onlines = CallRestService(url);
        LinearLayout layoutBase = FindViewById<LinearLayout>(Resource.Id.linearOnlineOffers);

        foreach (Online online in onlines)
        {
            ImageView img = new ImageView(this);
            img.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
            img.Visibility = ViewStates.Visible;
            layoutBase.AddView(img);
            Koush.UrlImageViewHelper.SetUrlDrawable(img, online.picture_filename);
        }
    }

我正在尝试将ImageView放在屏幕的整个宽度上。不幸的是,它看起来像这样:

enter image description here

我应该将图像更改为100%的屏幕?

谢谢!

1 个答案:

答案 0 :(得分:0)

我必须添加以下代码行:

img.SetScaleType(ImageView.ScaleType.CenterCrop);

所以最后代码将是这样的:

protected override void OnCreate(Bundle bundle)     {         base.OnCreate(束);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    string url = "http://192.168.90.102/test_api/handler.php/";

    Online[] onlines = CallRestService(url);
    LinearLayout layoutBase = FindViewById<LinearLayout>(Resource.Id.linearOnlineOffers);

    foreach (Online online in onlines)
    {
        ImageView img = new ImageView(this);
        img.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
        img.SetScaleType(ImageView.ScaleType.CenterCrop);
        img.Visibility = ViewStates.Visible;
        layoutBase.AddView(img);
        Koush.UrlImageViewHelper.SetUrlDrawable(img, online.picture_filename);
    }
}