【问题标题】:alignTop and alignBottom get trampled by android:srcalignTop 和 alignBottom 被 android:src 践踏
【发布时间】:2013-05-02 03:44:27
【问题描述】:

设置android:src 会破坏alignTop/alignBottom,因此ImageView 会增长到src 的大小。设置src后有没有办法再次强制对齐?

<RelativeLayout>

   <ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignTop="@+id/foo"
     android:layout_alignBottom="@+id/bar"
     android:src="@drawable/pewpew"
     android:scaleType="fitCenter" />

   <TextView
     android:id="@+id/foo" ... />
   <TextView
     android:id="@+id/bar" ... />

我希望它看起来像这样(Q 是 ImageView)

QQQ   foo TextView
QQQ   bar TextView

图像高度与 TextView 对齐的位置,并且是正方形。

【问题讨论】:

  • 你想用这样的布局做什么。
  • 我用粗图更新了

标签: android imageview alignment scale


【解决方案1】:

试试这个:

    <ImageView
        android:id="@+id/sheep"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sheep" />

    <TextView
        android:id="@+id/foo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="yyy" />

    <TextView
        android:id="@+id/bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/sheep"
        android:text="xxx" />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    我会尝试将 scaleType 设置为“center_inside”。

    【讨论】:

    • 这也被忽略了。我认为 scaleType 仅在专门设置宽度或高度时才有效。我猜 alignTop 和 alignBottom 不足以让它消失
    • 不幸。不能说我有任何相反的经验。当我过去遇到类似的问题时,我主要依赖于有点骇人听闻的解决方案。例如,您可以使用线性布局和负边距来实现相同的目标吗?你能以某种方式将图像设置为“match_parent”并调整父级的大小吗?等等。祝你好运。
    • 是的,可能会这样。可能将最大高度设置为操作栏的大小或其他东西。
    猜你喜欢
    • 1970-01-01
    • 2010-10-15
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2014-07-16
    • 1970-01-01
    相关资源
    最近更新 更多