Android ImageView不会match_parent宽度

发布于 2025-01-04 21:44:17 字数 426 浏览 3 评论 0原文

我有一个 1x20px 的 ImageView。该图像是 .9.png。我正在尝试使用 match_parent,但是当我在设备上运行该应用程序时,它不会拉伸以填充屏幕的宽度。以下是 ImageView 的 XML 代码

<ImageView
  android:id="@+id/dividerBar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:src="@drawable/ic_bar" 
  android:layout_gravity="center_horizontal"
  android:scaleType="centerInside" />

对于如何让 ImageView 覆盖屏幕的整个宽度有什么想法吗?

I have an ImageView that is 1x20px. The image is a .9.png. I am trying to use match_parent but when I run the app on my device, it is not stretched to fill the width of the screen. Here's the code in the XML for the ImageView

<ImageView
  android:id="@+id/dividerBar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:src="@drawable/ic_bar" 
  android:layout_gravity="center_horizontal"
  android:scaleType="centerInside" />

Any ideas on how I can get the ImageView to cover the entire width of the screen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2025-01-11 21:44:17

如果它是 9 补丁,则需要将其设置为 imageview 的背景属性,以使其正确缩放。现在它正在尝试使用您提供的scaleType 标记来缩放图像,就像缩放任何其他图像一样。

尝试设置

android:background="@drawable/ic_bar" 

并删除 src 属性

此外,如果您仅使用 imageView 来显示 9-patch,则可以切换到使用常规 View 而不是 ImageView。只要设置背景属性就可以了。

If it's a 9 patch it needs to be set as the background attribute of the imageview for it to scale properly. Right now it's trying to scale the image as it would any other image using the scaleType tag that you provided.

Try setting

android:background="@drawable/ic_bar" 

and remove the src attribute

Additionally, if you are using the imageView only to display the 9-patch you can switch to using a regular View instead of the ImageView. As long as you set the background attribute you will be good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文