【问题标题】:Scale ImageView to parents height while keeping aspect ratio在保持纵横比的同时将 ImageView 缩放到父母的高度
【发布时间】:2014-01-21 23:18:08
【问题描述】:

经过长时间的搜索,只找到了缩放 ImageView 宽度的结果,我希望有人可以帮助我解决我的问题。

我有两个 LinearLayout 在彼此下方,并在运行时用 24 个 ImageView 填充它们(每个 LinearLayout 12 个)。两种布局都占用相同的高度空间,现在我想缩放 ImageView,使它们与包含它们的 LinearLayout 具有相同的高度,同时保持 ImageView 内图像的纵横比。

我尝试知道许多不同的东西,例如 setScaleType、setAdjustViewBounds 或编写我自己的 ImageView 类并覆盖 onMeasure,但到目前为止,ImageView 无法从其父布局中获取整个可用高度。

也许我没有将正确的想法结合在一起,所以我希望有人可以帮助我解决这个问题。

编辑:

源图像的大小始终为 92 x 128。

布局:

firstCardHolder 和 secondCardHolder 是保存 ImageViews 的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
p1:orientation="vertical"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:id="@+id/contentHolder">
<LinearLayout
    p1:orientation="horizontal"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:id="@+id/timerLayout"
    p1:gravity="center"
    p1:layout_width="match_parent"
    p1:layout_height="wrap_content"
    p1:layout_alignParentTop="true"
    p1:layout_marginTop="10dp"
    p1:layout_marginLeft="5dp"
    p1:layout_marginRight="5dp"
    p1:layout_marginBottom="5dp">
    <TextView
        p1:text="03:00"
        p1:layout_width="wrap_content"
        p1:layout_height="match_parent"
        p1:id="@+id/timerTextView"
        p1:textSize="26dp" />
</LinearLayout>
<HorizontalScrollView
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:id="@+id/wordListScroll"
    p1:layout_width="match_parent"
    p1:layout_height="wrap_content"
    p1:layout_below="@+id/timerLayout"
    p1:layout_marginLeft="5dp"
    p1:layout_marginTop="5dp"
    p1:layout_marginRight="5dp"
    p1:layout_marginBottom="5dp">
    <LinearLayout
        p1:orientation="horizontal"
        p1:minWidth="25px"
        p1:minHeight="25px"
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/wordScrollLayout"
        p1:gravity="center_vertical" />
</HorizontalScrollView>
<LinearLayout
    p1:orientation="horizontal"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="match_parent"
    p1:layout_height="wrap_content"
    p1:id="@+id/footerLayout"
    p1:layout_marginLeft="5dp"
    p1:layout_marginRight="5dp"
    p1:layout_marginTop="5dp"
    p1:layout_marginBottom="15dp"
    p1:layout_alignParentBottom="true"
    p1:clipChildren="false"
    p1:clipToPadding="false">
    <TextView
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/hiraganaCombo"
        p1:gravity="center"
        p1:textSize="32dp" />
</LinearLayout>
<HorizontalScrollView
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:id="@+id/scrollLayout"
    p1:layout_width="wrap_content"
    p1:layout_height="match_parent"
    p1:layout_below="@+id/wordListScroll"
    p1:layout_marginTop="5dp"
    p1:layout_marginBottom="5dp"
    p1:layout_marginLeft="5dp"
    p1:layout_marginRight="5dp"
    p1:layout_above="@+id/footerLayout">
    <LinearLayout
        p1:orientation="vertical"
        p1:minWidth="25px"
        p1:minHeight="25px"
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/scrollContentLayout">
        <LinearLayout
            p1:orientation="horizontal"
            p1:layout_width="match_parent"
            p1:layout_height="match_parent"
            p1:layout_weight="0.5"
            p1:id="@+id/firstCardHolder"
            p1:background="#ff009411"
            p1:minWidth="25px"
            p1:minHeight="25px" />
        <LinearLayout
            p1:orientation="horizontal"
            p1:layout_width="match_parent"
            p1:layout_height="match_parent"
            p1:layout_weight="0.5"
            p1:id="@+id/secondCardHolder"
            p1:background="#ffd20000" />
    </LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

【问题讨论】:

    标签: android android-layout xamarin.android xamarin


    【解决方案1】:

    Try ImageView.ScaleType CENTER_INSIDE.

    “统一缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)都等于或小于视图的相应尺寸(减去填充)。”

    希望有帮助!

    【讨论】:

    • 我刚刚再次确认(已经试过了)。在我的测试设备上,它几乎很合适,但在每一行 ImageViews 下仍然留有一些空间。在具有与设备不同设置的模拟器上,ImageView 非常小,因此无法真正缩放到它们可以采用的高度。
    • 请为封闭视图添加布局代码。那里一定有问题。
    • 向问题添加布局!
    • 仍然不显示父级高度。请告诉我你为这些布局的高度编程了什么。
    • 我现在编辑了所有的布局xml代码,但也许我只是误解了你。我没有在这些 LinearLayout 的实际程序代码中编写任何内容。对于 ImageViews,我只是在运行时创建一个新的 ImageView 对象并将其作为源传递给它。
    【解决方案2】:

    解决了我在运行时获取父布局的高度并用它计算我的 ImageView 的宽度的问题。

    【讨论】:

      猜你喜欢
      • 2012-06-05
      • 2016-01-17
      • 2016-04-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多