动态 ImageView 100%的宽度在 android Xamarin

发布时间: 2017/3/4 6:59:14
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正与 Xamarin 和 Visual studio。我想要动态添加 ImageView 对我的应用程序从其他 service

我的 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:

我要添加以下代码行︰

img.SetScaleType(ImageView.ScaleType.CenterCrop);

所以最后的代码将像这样︰

受保护的重写无效 OnCreate ( Bundle bundle ) {基地。通过使用 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.SetScaleType(ImageView.ScaleType.CenterCrop);
        img.Visibility = ViewStates.Visible;
        layoutBase.AddView(img);
        Koush.UrlImageViewHelper.SetUrlDrawable(img, online.picture_filename);
    }
}
赞助商