【问题标题】:Auto-size ImageView (besides TextView) and parent height, while persevering aspect ratio自动调整 ImageView(TextView 除外)和父级高度,同时保持纵横比
【发布时间】:2013-12-01 21:32:14
【问题描述】:

我正在努力解决新手问题。

我有一个这样定义的布局:(它用作列表视图项)

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

    <ImageView
        android:id="@+id/articleSmallImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:adjustViewBounds="true"
        android:scaleType="fitStart" />

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_weight="2"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/newsIntroText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="TextView"
            android:textColor="@color/black" />

    </LinearLayout>
</LinearLayout>

我想要实现的是: 根据最高子级调整大小的父级布局高度。在这种情况下,如果 TextView 高于 ImageView,则 ImageView 纵横比应该保持不变,最大宽度可能,如果 ImageView i 高于 TextView,那么宽度应该是最大的,并且父级应该具有 ImageView 的高度。

这实际上是我的应用程序的屏幕截图,在 JB 4.3 上运行。看起来和预期的一样。但在 ICS 4.0.3、4.0.4 上看起来像这样。

我看不出 ImageVIew 没有填充该空白空间的任何原因。

帮助..

【问题讨论】:

  • 当您在水平线性布局中使用weight 时,您始终将width 设置为0dp
  • 我设置了 0dp 宽度值,但 ICS 没有变化

标签: android android-layout imageview aspect-ratio


【解决方案1】:

试试 scaleType 的 fitXY 值。顺便说一句,除非我遗漏了什么,否则我认为您的 LinearLayout1 没用。另外,就像优优说的那样。如果在垂直布局中设置了权重,则高度应为 0dp,而在水平线性布局中,应将宽度设置为 0dp,以获得更好的性能。

【讨论】:

  • LinearLayout1 包含多个 TextView,但这些项目设置为 GONE 可见性(我希望客户端请求在某个时候将它们置于 VISIBLE 状态),但它们的存在与我描述的问题无关.现在,我已将 scaleYpe 更改为 fitXY 并且正如我所料,图像被拉伸(宽度已填充),但高度很小(如第二个屏幕截图所示)。我已将宽度更改为 0dp。它虽然在 JB 上工作,但不在 ICS 上。 ICS 上的 adjustViewBound 有问题
  • ImageView的高度设置为match_parent。它现在没有缩放,因为它尝试使用的最大高度是图像的高度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
  • 2017-11-16
  • 2020-06-14
  • 2012-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多