2010-08-22 99 views
8

我正在嘗試將徽章添加到我的某個「活動」按鈕中。現在我正在嘗試執行xml。 用徽章的按鈕,應該是這樣的:在按鈕的左上方放置徽章氣泡

alt text

我已經做了泡沫和內部使用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" /> 

和這裏有兩個資源:

alt text alt text

我應該怎麼做的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> 

回答

7

使用(的RelativeLayout的替代)一個FrameLayout並把按鈕和圖像進去。

位置通過

android:layout_gravity="top|left" 
android:layout_marginTop="Xdp" 
android:layout_marginLeft="Xdp" 

到您喜歡的圖像(cirle與號)和按鈕。

+0

這看起來像正確的做法,但我仍然不能使它發揮作用。 將「marginTop」和「marginLeft」添加到徽章是有意義的,但它們應該是負值。這個位置在視野外。相反,我爲按鈕添加了正面的'marginTop'和'marginLeft',但這些值都被忽略。我還不明白爲什麼。 我會用最好的方法編輯這個問題。 – Macarse 2010-08-22 16:11:13

+0

是的,正確的。圖像沒有負邊距(雖然我還沒有嘗試過,如果這樣做也可以),你可以使用圖像和按鈕的頂部/左邊距,只要你喜歡。但這樣做確實有道理:FrameLayout的左上角位置是圖像的左上角,因此您需要將按鈕向右或向下移動一點。 – 2010-08-22 16:18:22

+0

很酷,向Button添加'android:layout_gravity =「top | left」'使它工作,但由於我的父寬度和高度都是「wrap_content」,所以它會切斷按鈕。我不想強制類似'android:layout_height =「60dp」'這看起來不錯。任何想法解決這個問題的更好方法? – Macarse 2010-08-22 16:29:30

3

我會說,應該是容易的FrameLayout:

<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" android:layout_marginTop="15dp"> 
     <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="15dip"> 
     <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_below="@id/coming_soon_button" 
     android:onClick="newReleaseClick"/> 
    </FrameLayout> 
    <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> 
</FrameLayout> 

也許你就需要調整頁邊距。

編輯:

添加android:layout_gravity="top|left"看起來就像這樣:

alt text

+0

這不適合我。徽章在按鈕上方顯示,「android:layout_marginTop」看起來不知何故被忽略。 – Macarse 2010-08-22 16:06:34

+0

奇怪..然後嘗試更新xml答案(你可能想調整內部FrameLayout的填充)。也可以嘗試使用RelativeLayout作爲頂級容器而不是FrameLayout。 – 2010-08-22 16:29:32

+0

確實在按鈕和RelativeLayout中添加了android:layout_gravity =「top | center」有什麼區別?代碼看起來一目瞭然。 – 2010-08-22 16:30:27