gpt4 book ai didi

java - 使用adjustViewBounds和scaleType后ImageView左右两边仍然出现空白

转载 作者:行者123 更新时间:2023-11-29 20:40:47 25 4
gpt4 key购买 nike

我是一个新的 android 程序员。我想用 PullToRefreshListView 显示图像,但我发现 ImageView 的左侧和右侧都有空白。我已经尝试了一些关于“ImageView 空白区域”的解决方案,包括使用 adjustViewBounds="true"scaleType,但它失败了。以下代码是我的列表单元格布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/userPortrait"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="@+id/timeAgo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20px"
android:layout_marginRight="5px"
/>


</LinearLayout>


<!-- here is my imageControl -->
<ImageView
android:id="@+id/imageUrl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>


</LinearLayout>

图片来自服务器,所以我通过imageView.setImageBitmap在代码中添加了图片源。我也试过使用imageView.setBackground(drawable),其实左右两边都没有出现空白,但是高度不对,不依赖于宽高比。

有人能帮帮我吗?提前致谢。

最佳答案

如果 ImageView 布局后的纵横比与图像本身的纵横比不同,你必须做出选择:

  • 如果您想将整个图像放入 ImageView 并留出一些空间(在您的例子中,左侧和右侧),请使用 android:scaleType="fitCenter"
  • 如果你想覆盖整个 ImageView 并裁剪图像(在你的例子中,在顶部和底部),使用 android:scaleType="cropCenter"
  • 如果您不希望有额外的空间或裁剪边,并且希望扭曲图像的纵横比以精确匹配 ImageView 尺寸,请使用 android:scaleType="fitXY ".

android:adjustViewBounds="true" 是这样工作的:当你有一个维度的 ImageView “固定”(例如 match_parent240dp 等)和另一个维度“灵活”(例如 wrap_content),设置 android:adjustViewBounds="true" 将导致 ImageView 布局使 wrap_content 尺寸恰到好处,以匹配源图像的纵横比。

由于您在 ImageView 的两个维度上都有 match_parentandroid:adjustViewBounds="true" 不会有任何效果。

关于java - 使用adjustViewBounds和scaleType后ImageView左右两边仍然出现空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31017690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com