方法一:使用开源项目ViewBadger,github上的地址:https://github.com/jgilfelt/android-viewbadger
效果如图所示:
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="文本1" />
tv = (TextView) findViewById(R.id.tv1);
BadgeView badgeView = new BadgeView(MainActivity.this, tv); //实例化BadgeView
badgeView.setText("12");
// badgeView.setTextSize(8.5f); //设置文字的大小
badgeView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);//设置在右上角
badgeView.setTextColor(Color.DKGRAY); //字体的设置颜色
badgeView.show(); //显示
这样就实现了上面的效果,注意引用开源项目ViewBadger时,要和新建的工程文件在同一个文件夹内,否则会出错的
方法二:用框架架构布局FrameLayout
效果如图所示:

布局如下:这样就可以了
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="文本2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:background="#FF0000"
android:text="23"
android:textColor="@android:color/white" />
</FrameLayout>