【问题标题】:ImageView width does not match parentImageView 宽度与父级不匹配
【发布时间】:2013-04-11 09:57:44
【问题描述】:

我有什么: ScrollView,在里面 LinearLayout

(orientation="vertical", layout_width="match_parent", layout_height="wrap_content"),

里面

ImageView

(layout_width="match_parent", layout_height="wrap_content") 和

TextView.

我想要什么: ImageView 应该保持纵横比和数学父宽度,以及 ImageView 下面的文本。

我使用了 ImageView params 的不同组合:adjustViewBounds true/false 和所有 scaleType,但我无法得到我想要的:

  1. 如果我使用 scaleType centerCrop - 如果图像被裁剪,则为顶部和底部。
  2. 如果我使用 scaleType center - 左右空白
  3. 如果我使用 scaleType fitXY - 图像不保持纵横比

所以我不能让图像更高,我该如何解决?

【问题讨论】:

  • 您能否实际发布您的布局 xml 以及显示您所获得的内容和应该是什么样子的屏幕?阅读代码比通过描述获取代码要容易得多......
  • 图片小于父级怎么办?是否应该扩大规模?这会扭曲图像。
  • 为您的 ImageView 尝试scaleType="fitCenter"

标签: android android-layout imageview


【解决方案1】:

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_main"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       >
<ImageView
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:id="@+id/iv"
        android:background="#f0f"
        />  
<TextView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="Always work!!"
           android:gravity="center"
           android:textSize="26sp"
           />
</LinearLayout>
</ScrollView>

注意:如果scaleType 不起作用,请尝试使用android:background="" 而不是android:src=""

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2018-10-05
    • 2015-10-13
    • 2017-05-25
    • 2023-03-23
    相关资源
    最近更新 更多