相对布局背景图与内容重叠问题

3

我有一个9-patch drawable(date_time)。我想把这个drawable放在Relative Layout内容的后面,这样所有的子视图都应该画在这个图片的上面。以下是xml布局:

<RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="23dp"
            android:layout_below="@id/tv_map_address"
            android:layout_marginTop="11dp"
            android:layout_marginBottom="12dp"
            android:background="@drawable/date_time">

            <ImageView
                android:id="@+id/iv_map_distance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_map_distance"/>

            <TextView
                android:id="@+id/tv_map_distance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@id/iv_map_distance"
                android:layout_centerVertical="true"
                android:textColor="@color/white"
                android:textSize="11sp"
                android:text="2,7 км"
                fontPath="fonts/Roboto-Medium.ttf"/>

            <ImageView
                android:id="@+id/iv_map_path"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@id/tv_map_distance"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_map_path"/>

            <TextView
                android:id="@+id/tv_map_path"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@id/iv_map_path"
                android:layout_centerVertical="true"
                android:textColor="@color/white"
                android:textSize="11sp"
                android:text="3,2 км"
                fontPath="fonts/Roboto-Medium.ttf"/>

        </RelativeLayout>

但不知何故,这个背景会覆盖所有子内容。以下展示了发生的情况。

Overlapping content

但如果我替换了

标签

android:background="@drawable/date_time"

android:background="#0000FF"

一切都很好,输出如下:

enter image description here

你能解释一下我做错了什么吗?

更新: 这是我的9-patch可绘制对象。

enter image description here


发布你的9-patch可绘制对象,内容指示器可能不正确。 - Bartek Lipinski
@blipinsk 我已经添加了drawable。如果你需要测试,我也可以将它上传到一些图像主机上。 - Anatol
1个回答

3

正如我所想,你的内容指标是不正确的(除非你想用9-patch来填充这样的内容)。请尝试使用以下方法:

9-patch

右下方的指南会决定你的drawable应该占用多少空间来显示你的内容。你将这个区域设置得非常小,并且固定了你的Layout的高度。换句话说:9-patch强制让你的Layout只让其子元素占用一个很小的区域(这个区域是由于你没有让Layout进行拉伸而限制了大小的)。


是的,你完全正确。最终我明白了这个 9-patch 的魔法是如何工作的。谢谢。 - Anatol
太棒了,很高兴我能帮到你 :) - Bartek Lipinski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接