2017-07-27 121 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