android Xamarin 中的动态 ImageView 100% 宽度
Dynamic ImageView 100% width in android Xamarin
我正在使用 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 位于屏幕的整个宽度上。不幸的是,它看起来像这样:
我应该更改什么才能使图像 100% 占据屏幕?
谢谢!
我必须添加以下代码行:
img.SetScaleType(ImageView.ScaleType.CenterCrop);
所以最后的代码将是这样的:
受保护的覆盖 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);
}
}
我正在使用 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 位于屏幕的整个宽度上。不幸的是,它看起来像这样:
我应该更改什么才能使图像 100% 占据屏幕?
谢谢!
我必须添加以下代码行:
img.SetScaleType(ImageView.ScaleType.CenterCrop);
所以最后的代码将是这样的:
受保护的覆盖 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);
}
}