在Android中像iPhone一样绘制数字徽章

Dar*_*ati 9 android android-tabhost

一只忙碌的猫

我想在"聊天"选项卡上绘制上图中所示的数字徽章.该号码标记显示聊天中新未读邮件的数量.那么我该如何绘制这种自定义数字徽章呢?Android SDK中是否有任何内置API?提前致谢.

cch*_*son 15

看看我在这里为小部件/应用程序的答案.

您可以使用 shapecount.xml

RES /布局/ tabicon.xml

<RelativeLayout
    android:orientation="vertical"
    android:background="@null"
    android:id="@+id/rlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageView 
            android:id="@+id/icon"
            android:src="@android:drawable/ic_menu_mylocation" <!-- Just for test -->
            android:layout_margin="0dp"
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content"/>

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="50" android:textSize="12dp" android:textStyle="bold"  
        android:background="@drawable/shapecount"
        android:textColor="#FFFFFF"
        android:paddingLeft="3dp" android:paddingRight="3dp"
            android:layout_margin="0dp"
        android:layout_alignBottom="@+id/rlayout"
        android:id="@+id/txtCount" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

创建标签时:

LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.tabicon, null);
final TextView txtCount = (TextView) view.findViewById(R.id.txtCount);

spec = tabHost.newTabSpec("artists").setIndicator(view).setContent(intent);
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用txtCount更新选项卡图标中的数字.请参阅上面我指出的帖子,以获取TimerTask的测试结果.

您还必须根据需要安排布局