2012-06-18 12 views
6

請參閱以下內容:在Android中,我如何將圖像置於另一個之上看起來像徽章?

iPhone badges

我嘗試使用絕對佈局,但是這不推薦使用。感謝您的幫助,謝謝。

+6

永遠不會*,永遠*,***永遠***使用'AbsoluteLayout'。永遠。 –

+2

@AlexLockwood:那麼你想說的是人們不應該使用'AbsoluteLayout'?只是想我會檢查。 ; D – Squonk

+1

@Squonk,當涉及到這些東西時,沒有「過於強調」的東西......尤其是在StackOverflow上:P –

回答

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(或ViewGroupLinearLayoutRelativeLayout),並給它一個背景繪製和將文本設置爲你想要的。

+0

謝謝,那太棒了。 – StackOverflowed

+0

@Sam Dozor我如何獲得徽章imageview在第一個ImageView的外部顯示(它當前顯示在imageview圖標的內部)。 –

7

看看github上的ViewBadger項目(但請記住,您不應該嘗試複製Android應用中的其他平臺UI元素)。

+2

+1不要複製其他平臺UI! –

+0

我通常討厭+1,但+1。在那裏,做到了,一個痛苦的世界 - 永遠不會。 – Simon

相關問題