【问题标题】:Unable to set Image from Resource to Custom ImageView in Xamarin Android?无法在 Xamarin Android 中将图像从资源设置为自定义 ImageView?
【发布时间】:2019-04-27 09:40:42
【问题描述】:

我可以在非自定义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)
    {

    }
}

【问题讨论】:

  • 在自定义 ImageTestView 类中你没有做任何事情的第一件事。
  • 如果使用你的代码会有问题。你的自定义imageview中不会有构造函数。你可以参考G.hakim的回答。
  • 我使用了 hakim 的代码,但同样的问题
  • 您为什么不尝试将图像移动到可绘制文件夹,然后检查从该文件夹中使用它是否有效?

标签: android android-activity view xamarin.android imageview


【解决方案1】:

更新

最后,通过 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?.SetImageResource(Resource.Mipmap.line_indent); 并查看 ImageView 是否存在
  • 是的,它可以正常使用ImageView,但不能使用自定义图像视图,但我认为图像已加载但不可见。
  • 我已将 ImageView 的背景颜色设置为红色并显示颜色。
  • 所以问题在于设置图像而不是您的 ImageView 我建议您尝试从 XML 设置背景属性,看看是否有帮助! android:background="@mipmap/imagename"
  • @VINNUSAURUS 如果是这样,我建议您将此标记为正确答案。
【解决方案2】:

1.首先你在自定义 ImageTestView 类中没有做任何事情。

2.设置图片资源是错误的。你必须从Drawable文件夹中获取它应该是。

imageView.SetImageResource(Resource.Drawable.line_indent);

我不知道 Xamarin Android。但是如果你知道 Android 请参考这个例子。它有点类似于 xamarin Android

http://www.java2s.com/Code/Android/UI/extendsImageView.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多