我正在嘗試將徽章添加到我的某個「活動」按鈕中。現在我正在嘗試執行xml。 用徽章的按鈕,應該是這樣的:在按鈕的左上方放置徽章氣泡
我已經做了泡沫和內部使用RelativeLayout
文:
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/badge"
android:layout_centerInParent="true"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="2"
android:layout_centerInParent="true" />
</RelativeLayout>
但我不能找到一種方法把它放在那裏,並使用相同的xml在縱向和橫向上工作。
的Buttoms在Activity
是這樣的:
<Button android:id="@+id/new_releases_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_button_selector"
android:text="@string/new_releases_title"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:gravity="center_vertical"
android:paddingLeft="12dp"
android:layout_marginTop="15dp"
android:layout_below="@id/coming_soon_button"
android:onClick="newReleaseClick"
android:layout_centerHorizontal="true" />
<Button android:id="@+id/top_sellers_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_button_selector"
android:text="@string/top_sellers_title"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:gravity="center_vertical"
android:paddingLeft="12dp"
android:layout_marginTop="15dp"
android:layout_below="@id/new_releases_button"
android:onClick="topSellersClick"
android:layout_centerHorizontal="true" />
和這裏有兩個資源:
我應該怎麼做的XML?
編輯: 最好的方法,到目前爲止,但它仍然不能正常工作:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/discounts_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_button_selector"
android:text="@string/discounts_title"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:onClick="discountsClick"
android:layout_marginTop="40dp"
android:layout_marginLeft="20dp"/>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/badge"
android:layout_centerInParent="true"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="20"
android:layout_centerInParent="true" />
</RelativeLayout>
</FrameLayout>
這看起來像正確的做法,但我仍然不能使它發揮作用。 將「marginTop」和「marginLeft」添加到徽章是有意義的,但它們應該是負值。這個位置在視野外。相反,我爲按鈕添加了正面的'marginTop'和'marginLeft',但這些值都被忽略。我還不明白爲什麼。 我會用最好的方法編輯這個問題。 – Macarse 2010-08-22 16:11:13
是的,正確的。圖像沒有負邊距(雖然我還沒有嘗試過,如果這樣做也可以),你可以使用圖像和按鈕的頂部/左邊距,只要你喜歡。但這樣做確實有道理:FrameLayout的左上角位置是圖像的左上角,因此您需要將按鈕向右或向下移動一點。 – 2010-08-22 16:18:22
很酷,向Button添加'android:layout_gravity =「top | left」'使它工作,但由於我的父寬度和高度都是「wrap_content」,所以它會切斷按鈕。我不想強制類似'android:layout_height =「60dp」'這看起來不錯。任何想法解決這個問題的更好方法? – Macarse 2010-08-22 16:29:30