【问题标题】:Drawing number badge like iPhone in Android在 Android 中像 iPhone 一样绘制数字徽章
【发布时间】:2011-05-18 12:43:36
【问题描述】:

我想在“聊天”选项卡上绘制如上图所示的数字徽章。该数字徽章显示聊天中新的未读消息的数量。那么我该如何绘制这种自定义数字徽章。 Android SDK中是否有任何内置API?提前致谢。

【问题讨论】:

    标签: android android-tabhost


    【解决方案1】:

    查看我的回答 here 获取小部件/应用程序。

    你可以使用shapecount.xml

    res/layout/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>
    

    创建标签时:

    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);
    

    现在您可以使用txtCount 来更新标签图标中的数字。请参阅我上面指出的帖子以获取 TimerTask 的示例来对此进行测试。

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

    【讨论】:

    • 嗨,实现徽章的好方法,但我遇到了一个问题,当使用自定义视图作为选项卡指示器时,视图的背景应该与其他选项卡背景匹配,我如何访问默认选项卡指标可绘制??
    • 最后我侵入了 sdk 并得到了 tab_indicator.xml 和其他可绘制对象......
    • 像 iphone 这样的徽章在 android 中制作起来非常简单...感谢您的想法,,, 我什至不知道我们可以在选项卡的指示器中设置视图:P
    【解决方案2】:

    您可以通过使用 FrameLayout 来实现这一点:Common Layout Objects

    【讨论】:

    • 这根本不能回答问题
    【解决方案3】:

    有一个方便的library 可以轻松实现这一点。只需一个 jar 文件即可添加到您的项目中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 2011-06-17
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多