在Android中,我如何将一张图片定位在另一张图片的上方,看起来像个徽章?

7
请看下面:

iPhone标志

我尝试使用绝对布局,但这已被弃用。感谢您的帮助,谢谢。


6
永远不要、绝对不能、万万不可使用AbsoluteLayout布局。 - Alex Lockwood
2
@AlexLockwood:所以你想说的是人们不应该使用AbsoluteLayout吗?只是想确认一下。;D - Squonk
1
@Squonk,在这些事情上,特别是在StackOverflow上,强调得越多越好... :P - Alex Lockwood
2个回答

16

RelativeLayout 是一个不错的选择。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:scaleType="centerCrop"
    android:src="@drawable/iconImage" />

<ImageView
    android:id="@+id/badge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/icon"
    android:layout_alignTop="@+id/icon"
    android:src="@drawable/badge" />
如果你想要一个带有动态数字或文本的徽章,那么你可以将第二个 ImageView 改为 TextView(或者像 LinearLayoutRelativeLayout 这样的 ViewGroup),并给它一个背景drawable来设置显示的文本。

@Sam Dozor 我该如何让徽章 ImageView 显示在第一个 ImageView 的外部(它目前显示在 ImageView 图标内部)? - hemanth kumar

8

请查看github上的ViewBadger项目(但请记住不要在Android应用程序中复制其他平台的UI元素)。


我通常讨厌+1,但是这次要用。之前尝试过,痛不欲生,再也不想了。 - Simon

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