无法在 Xamarin Android 中将资源中的图像设置为自定义 ImageView?
Unable to set Image from Resource to Custom ImageView in Xamarin Android?
我可以在非自定义 ImageView
中设置图像,但无法在 Custom ImageView
中设置图像,应用加载成功但无法看到 Image
,下面是代码.
public class MainActivity : AppCompatActivity
{
public ImageTestView imageView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
imageView = FindViewById<ImageTestView>(Resource.Id.imageView1);
imageView.SetImageResource(Resource.Mipmap.line_indent);
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageViewTestProject.ImageTestView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1" />
</RelativeLayout>
public class ImageTestView : ImageView
{
public ImageTestView(Context context,IAttributeSet attr) :
base(context,attr)
{
}
}
1.First 你没有在自定义中做任何事情 ImageTestView class.
2.And 设置图像资源是 wrong.either 你必须从 Drawable 文件夹中获取它应该是。
imageView.SetImageResource(Resource.Drawable.line_indent);
我不知道 Xamarin Android.But 如果你知道 Android 请参考这个 Example.it 有点类似于 xamarin Android
更新
最后通过XML添加图片背景,问题解决。
android:background="@mipmap/imagename"
嗯,我认为它不起作用的原因是构造函数不可用和初始化不正确
public class ImageTestView : ImageView
{
Context mContext;
public ImageTestView (Context context) : base(context)
{
init(context, null);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
{
init(context, attrs);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
init(context, attrs);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
init(context, attrs);
}
private void init(Context ctx, Android.Util.IAttributeSet attrs)
{
mContext = ctx;
}
}
然后在您的应用中使用此自定义图像视图,例如:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageViewTestProject.ImageTestView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1" />
</RelativeLayout>
然后你可以这样设置图片资源:
var imageView = FindViewById<ImageTestView>(Resource.Id.imageView1);
imageView?.SetImageResource(Resource.Mipmap.line_indent);
如果这不起作用或有疑问,请还原
我可以在非自定义 ImageView
中设置图像,但无法在 Custom ImageView
中设置图像,应用加载成功但无法看到 Image
,下面是代码.
public class MainActivity : AppCompatActivity
{
public ImageTestView imageView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
imageView = FindViewById<ImageTestView>(Resource.Id.imageView1);
imageView.SetImageResource(Resource.Mipmap.line_indent);
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageViewTestProject.ImageTestView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1" />
</RelativeLayout>
public class ImageTestView : ImageView
{
public ImageTestView(Context context,IAttributeSet attr) :
base(context,attr)
{
}
}
1.First 你没有在自定义中做任何事情 ImageTestView class.
2.And 设置图像资源是 wrong.either 你必须从 Drawable 文件夹中获取它应该是。
imageView.SetImageResource(Resource.Drawable.line_indent);
我不知道 Xamarin Android.But 如果你知道 Android 请参考这个 Example.it 有点类似于 xamarin Android
更新
最后通过XML添加图片背景,问题解决。
android:background="@mipmap/imagename"
嗯,我认为它不起作用的原因是构造函数不可用和初始化不正确
public class ImageTestView : ImageView
{
Context mContext;
public ImageTestView (Context context) : base(context)
{
init(context, null);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
{
init(context, attrs);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
init(context, attrs);
}
public ImageTestView (Context context, Android.Util.IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
init(context, attrs);
}
private void init(Context ctx, Android.Util.IAttributeSet attrs)
{
mContext = ctx;
}
}
然后在您的应用中使用此自定义图像视图,例如:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageViewTestProject.ImageTestView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1" />
</RelativeLayout>
然后你可以这样设置图片资源:
var imageView = FindViewById<ImageTestView>(Resource.Id.imageView1);
imageView?.SetImageResource(Resource.Mipmap.line_indent);
如果这不起作用或有疑问,请还原