如何解决如何在 Android Studio For Android Noutgat 及以下版本的 Image View 中放置图像
我的项目有图像视图。它不能在 Android Noutgat 及以下版本中工作。但它可以适用于 Android 8.0 及更高版本。我该如何解决。
MyActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="MainActivity"
android:background="@color/Color_white"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:background="@drawable/backroundlegend"
/>
</RelativeLayout>
当我删除 imageView 时,它可以适用于 Android Noutgat 及以下
解决方法
Android 版本与您的情况无关。
当您将图像设置为 ImageView
时,您需要像 Picasso、Glide、Fresco、Coil 等图像加载器库来加载图像以编程方式,如果您没有像这样设置图像,则图像的显示质量非常低,或者在大多数情况下,图像不会加载。您可以使用任何一种,但我认为 Glide 是最受欢迎的。
您需要做的是给 ImageView
一个 ID,在与此布局关联的 Java/Kotlin 类文件中访问它,并使用这些库中的任何一个将图像设置为它。
但首先,您必须在项目中实现该库。请参阅我在上面链接的这些库页面上的实施说明。
然后,将 ID imageView
设为
<ImageView
android:id="@+id/imageView"
...
/>
然后,在 Java/Kotlin 文件中访问它
//Kotlin
val imageView = findViewById(R.id.imageView)
//Java
ImageView imageView = findViewById(R.id.imageView);
然后,将您的图片设置为
//this is the Context here for activity,use context or requireContext() in Fragment.
Glide.with(this).load(R.drawable.backgroundlegend).into(imageView)
此后,您的图像将出现。请记住这一点,当您设置 svg 和 drawables 时,您不需要以编程方式加载图像,但是当您使用 png、jpg/jpeg 等图像时,您必须使用图像加载器。此外,您可以使用 scaleType
的 ImageView
属性来调整 ImageView
视图边界中的图像。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。