1

我想在我的 Android 电子阅读器应用程序上有一个“评论”按钮,显示图标内当前发布的评论数量......所以基本上我想要一个里面有一个数字的评论气泡。

int number_of_comments我可以有多个可绘制对象,每个都是内部具有不同数字的气泡,并使用 switch 语句根据正在显示的元素的字段选择每次加载哪个 。不过,这种方法似乎有点浪费,而且无论如何我觉得有一种更优雅的方法可以做到这一点。

有任何想法吗?

4

2 回答 2

4

你可以做得更好。您可以在图像视图顶部有一个文本视图,并在每次添加新评论时不断更新其值。您可以像下面这样在 xml 中定义重叠,并相应地调整代码逻辑以增加评论数。现在我刚刚设置了一个虚拟文本 Hello 显示在 ImageView 的顶部。您可以使用 TextView 的 setText 方法添加您的评论数。

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myImageSouce" />

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/myImageView"
    android:layout_alignTop="@+id/myImageView"
    android:layout_alignRight="@+id/myImageView"
    android:layout_alignBottom="@+id/myImageView"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="#000000" />

</RelativeLayout>

希望这可以帮助...

于 2013-02-26T02:24:02.287 回答
3

看看这个第 3 方创建的小部件

Android ViewBadger

我认为您可以使用它来创建您正在寻找的小数字气泡。这给您带来的好处是无需过多地修改布局来实现您想要获得的效果。

这是应用“徽章”的示例代码

View target = findViewById(R.id.target_view);
BadgeView badge = new BadgeView(this, target);
badge.setText("1");
badge.show();
于 2013-02-26T02:26:18.030 回答