【问题标题】:Extra left and right padding on scaled ImageView缩放后的 ImageView 上的额外左右填充
【发布时间】:2014-06-22 11:21:53
【问题描述】:

我有这个布局:

    <FrameLayout
        android:id="@+id/artwork_detected_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="20dip"
        android:layout_marginLeft="18dip"
        android:layout_marginTop="20dip"
        android:padding="6dip" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/txt_favourites"
            android:textColor="@color/welcome_textcolor"
            android:textSize="24sp" />
    </FrameLayout>

    <ImageView
        android:id="@+id/btn_detect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/artwork_detected_text"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/artwork_detected_text"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:src="@drawable/detect_btn" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/artwork_detected_text"
        android:layout_alignTop="@id/artwork_detected_text"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/btn_detect"
        android:adjustViewBounds="true"
        android:src="@drawable/back_btn" />
</RelativeLayout>

这是在eclipse图形布局中的样子

如果您注意到右侧两个 ImageView 的左侧和右侧有多余的填充。 我如何在没有

的情况下删除它
  • 为图像指定绝对宽度/高度(我希望它们从左侧的 TextView 中对齐顶部和对齐底部)

  • 具有负边距/填充(必须根据屏幕密度进行调整)

我已经将adjustViewBounds属性设置为true,你可以在代码中看到,但显然它只修复了ImageViews顶部和底部不需要的填充,所以它在这里基本上没用。

附加信息: 图像很大 - 比显示的要大得多。但是正如您在此处看到的那样,它们的缩放比例正确。不,它们没有透明填充。

【问题讨论】:

  • 好奇:你在实际设备上试过了吗?图形编辑器有时会有点问题。
  • 是的,它看起来和eclipse预览中的一样。
  • 您可以尝试删除 alignTop 和 alignBottom 属性吗?它们不应该与适当的 centerVertical 属性相关,并且可能会发生冲突。
  • 它们是相关的,因为我希望高度相对于左侧 TextView 的大小。除非有其他方法可以做到这一点?
  • 哦,对不起,我忘了你的图片更大了;只是试图缩小范围。也许尝试将 textview 权重设置为 1 的水平 LinearLayout?

标签: android imageview padding


【解决方案1】:

您可以像这样在 RelativeLayot 中使用 LinerLayout,例如:

<RelativeLayout
    android:id="@+id/top_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/favorites_top_bar" >

    <FrameLayout
        android:id="@+id/artwork_detected_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="20dip"
        android:layout_marginLeft="18dip"
        android:layout_marginTop="20dip"
        android:padding="6dip" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/txt_artword_detected"
            android:textColor="@color/welcome_textcolor"
            android:textSize="24sp" />
    </FrameLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/artwork_detected_text"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/artwork_detected_text"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/artwork_detected_text"
        android:background="@color/favorites_top_bar"
        android:gravity="center_vertical|right"
        android:orientation="horizontal"
        android:paddingRight="10dip" >

        <ImageView
            android:id="@+id/btn_detect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/detect_btn" />

        <View
            android:layout_width="6dip"
            android:layout_height="wrap_content" >
        </View>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/back_btn" />
    </LinearLayout>
</RelativeLayout>

【讨论】:

  • 只是做了一些非常细微的改变,但效果很好!现在我必须知道,为什么这行得通而我原来的实现却不行?
  • 有趣的问题。我认为整个事情在不同的布局渲染工作。就我而言,这是通过经验进行的直观理解。无论如何,我不知道这个问题的确切答案。在空闲时间,我将尝试查看源代码以了解这一点。但这是一个非常有趣的问题,也许其他人会回答它。
  • 你是说 LinearLayout 和 RelativeLayout 对待其子 ImageViews 的方式不同.. 很奇怪,但看起来这就是这里发生的事情。
猜你喜欢
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-13
  • 2011-03-20
相关资源
最近更新 更多