2017-07-27 122 views
0

我有一個FlowLayout,用戶可以在其中添加口味,如音樂,遊戲,運動等。用戶通知他想添加的內容後,他單擊一個按鈕以在流佈局中顯示它,所以,這個過程必須以編程方式完成。創建一個圖像,設置可繪製和大小。我做了一些。但現在我需要顯示一個圖像視圖和一個按鈕,以便用戶可以刪除添加的味道。我認爲在xml中創建這個過程將無濟於事,因爲用戶可能不會添加任何口味。在ImageView上設置按鈕 - Android

我已經做了:

enter image description here

我必須做到:

enter image description here

方法我用:

ImageView iconLike = new ImageView(Register30.this); 
        iconLike.setImageResource(getIconLike(like)); 
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(getSizeIconLike()); 
        lp.setMargins(5,5,5,5); 
        iconLike.setLayoutParams(lp); 
        Like newLike = new Like(); 
        newLike.setIcon(iconLike); 
        newLike.setGenderFather(null); 
        newLike.setGenderChild(null); 
        newLike.setName(like); 
        likes.add(newLike); 
        likesContainer.addView(iconLike); 
+2

可能重複的[如何將按鈕放在Android圖像?](https://stackoverflow.com/questions/14211484/how-to-place-buttons-over-image-in-android) –

+0

您應該創建他們在XML中,並隱藏它們,並使它們以編程方式無法點擊。 –

+0

但味道的數量是未知的。用戶可以添加很多或沒有人。當用戶搜索它時,味道就會存在,然後點擊一個按鈕來添加它。 – Adolfok3

回答

0

添加在xml 2文本視圖與圈子的背景

<FrameLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 


    <ImageButton 
     android:id="@+id/taste" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    <TextView 
     android:id="@+id/remove" 
     android:layout_width="width you want" 
     android:layout_height="height you want" 
     android:gravity="center" 
     android:layout_gravity="end" 
     android:background="@drawable/circle_background_with 
     _your_color" 
     android:visibility="gone"/> 

    <TextView 
     android:id="@+id/add" 
     android:layout_width="width you want" 
     android:layout_height="height you want" 
     android:gravity="center" 
     android:layout_gravity="start" 
     android:background="@drawable/circle_background_with 
     _your_color" 
     android:visibility="gone"/> 

</FrameLayout> 

當用戶長時間點擊一個味道,只是改變了文本視圖的可見性。您需要在口味適配器中設置每種口味的長按功能。在這個功能中你可以在狀態之間切換。

+0

但味道的數量是未知的。用戶可以添加很多或沒有人。當用戶搜索它時,味道就會存在,然後點擊一個按鈕來添加它。 – Adolfok3

+0

因此,添加一個狀態,如果用戶搜索它不顯示按鈕... – TzlilPha