【发布时间】:2016-10-04 05:23:51
【问题描述】:
我试图将一个小的 ImageView 放在一个按钮的顶部,但不知何故它一直在它下面。我不明白为什么这种布局与其他视图完美配合。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_white_clickable"
android:text="@string/book_now"
android:id="@+id/but_book_now"
/>
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/img_bookmark"
android:src="@drawable/ic_action_bookmark_full"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
我尝试过为按钮设置不同的背景,但图像只有在透明时才能看到。我也试过用彩色视图替换 ImageView,还是不行。
有什么想法吗?谢谢
编辑:
我得到的答案很混乱,我的意思是让 ImageView 按 Z 顺序而不是 y 轴在 Button 顶部。
编辑 2:
我不想使用图像按钮,图像只是按钮上会出现/消失的“图钉”。 我不想在按钮上使用drawable,我需要将它放在我想要的位置。 使用 FrameLayout 而不是 RelativeLayout 不会改变任何东西。 使用 imgView.bringToFront() 不起作用。
编辑 3:
在 Lollipop 之前,该问题不会出现在设备上。在 KitKat 上,布局按预期工作。
【问题讨论】:
-
你需要把图片放在按钮上面吗?
-
你试过 ImageButton 吗? developer.android.com/reference/android/widget/ImageButton.html
-
如果你想要一个按钮上的图像,ImageButton 可能是一个更好的主意。无论如何,在相对布局中,z 顺序是由文件中视图的顺序完成的。因此,如果图像是第二个定义的,它应该绘制在按钮的顶部。我们可能难以准确理解您想要什么,您拥有和想要什么的图片是个好主意。
-
@Shadow 是的,在顶部按 z 顺序,而不是 y 轴。
-
找到了解决方法。将 ImageView 的高度设置为 1dp。检查这个。 stackoverflow.com/questions/28105551/…
标签: java android android-layout