C# android Xamarin中的Dynamic ImageView 100%宽度
我正在与Xamarin和Visual studio合作。我想从REST服务在我的应用程序上动态添加ImageView 我的Main.axml如下所示:C# android Xamarin中的Dynamic ImageView 100%宽度,c#,android,visual-studio,xamarin.android,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
<?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>
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);
}
}
我的MainActivity.cs如下所示:
<?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>
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);
}
}
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
字符串url=”http://192.168.90.102/test_api/handler.php/";
Online[]onlines=CallRestService(url);
LinearLayout layoutBase=FindViewById(Resource.Id.linearOnlineOffers);
foreach(在线中的在线)
{
ImageView img=新的ImageView(本);
img.LayoutParameters=新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.MatchParent);
img.Visibility=ViewStates.Visible;
layoutBase.AddView(img);
Koush.urlmageviewHelper.SetUrlDrawable(img,online.picture\u文件名);
}
}
我试图使ImageView位于屏幕的整个宽度上。不幸的是,它看起来像这样:
<?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>
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);
}
}
要使图像100%显示在屏幕上,我应该更改什么
谢谢大家! 我必须添加以下代码行:
img.SetScaleType(ImageView.ScaleType.CenterCrop);
因此,最终代码如下所示:
<?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>
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);
}
}
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle)
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
字符串url=”http://192.168.90.102/test_api/handler.php/";
Online[]onlines=CallRestService(url);
LinearLayout layoutBase=FindViewById(Resource.Id.linearOnlineOffers);
foreach(在线中的在线)
{
ImageView img=新的ImageView(本);
img.LayoutParameters=新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.MatchParent);
img.SetScaleType(ImageView.ScaleType.CenterCrop);
img.Visibility=ViewStates.Visible;
layoutBase.AddView(img);
Koush.urlmageviewHelper.SetUrlDrawable(img,online.picture\u文件名);
}
}