我有一个Android应用程序,其中主屏幕是标签视图。标签之一用于“收件箱”活动。我想显示一个指示数字新消息的数字。这似乎是iPhone应用程序上的标准功能。是否有任何系统支持?如果不是,那么对Android TabView实施此效果的最简单方法是什么?

有帮助吗?

解决方案

根据我在Android Sourcecode(Tabhost.java)中可以阅读的内容,无法以类似的方式使用标准小部件进行类似的iPhone。

您能做的是:

  1. 每当更改收件箱计数时,请使用 http://developer.android.com/reference/android/widget/tabhost.html#clearalltabs()
  2. 然后,使用 http://developer.android.com/reference/android/widget/tabhost.html#addtab(Android.widget.tabhost.tabspec),但请确保使用[http://developer.android.com/reference/reference/android/widget/widget/tabhost.tabspec.html#setiondicator(java.lang.lang.Charsequence,android.graphics.drawable.drawable.drawable.drawable.drawable) 1]。您可以在那里通过自己的绘图,并用它自己画徽章。

不过,这并不能替代iPhone风格的徽章,因为iPhone徽章在标签的最佳角落非常好。到目前为止,没有办法更改它。

另一方面,这是所有的Java,有人可以扩展TABHOST类并增加对iPhone样式徽章的支持。

[1]: http://developer.android.com/reference/android/widget/tabhost.tabspec.html#setindicator(java.lang.charsecorence, ,android.graphics.drawable.drabable)

其他提示

这是一个例子 如何在标签中添加徽章

chat_tab.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip" 
    android:layout_height="64dip"
    android:layout_weight="1" 
    android:layout_marginLeft="-3dip" 
    android:layout_marginRight="-3dip" 
    android:orientation="vertical" 
    android:background="@drawable/tab_indicator" >

    <ImageView
        android:id="@+id/chat_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/chat_icon"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/new_notifications" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/chat_icon"
        android:layout_toRightOf="@+id/chat_icon"
        android:layout_marginLeft="-8dp"
        android:layout_marginTop="0dp"
        android:paddingTop="2dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingBottom="2dp"
        android:textSize="8sp"
        android:textStyle="bold"
        android:textColor="@android:color/primary_text_dark"
        android:background="@drawable/badge"
        android:visibility="gone"/>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chat"
        style="@android:attr/tabWidgetStyle"
        android:textColor="@android:color/tab_indicator_text"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"/>


</RelativeLayout>

这是 badge.xml (通知背景的红色圆圈),文本视图ID:new_notifications背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval" >

    <stroke android:width="2dp" android:color="#FFFFFF" />

    <corners android:radius="10dp"/>

    <padding android:left="2dp" />

    <solid android:color="#ff2233"/>

</shape>

然后,在代码中,您可以简单地做

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View chatTab = inflater.inflate(R.layout.chat_tab, null);

        tvNewNotifications = (TextView) chatTab.findViewById(R.id.new_notifications);

        intent = new Intent().setClass(MainTab.this, Chat.class);
        tabSpec = tabHost
                .newTabSpec("chat")
                .setIndicator(chatTab)
                .setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

如您所见,我的相对布局具有背景 @drawable/tab_indicator选项卡指示器.xml 是我从SDK获得的框架标准标准,我建议您还应该从SDK中的API文件夹中获取它,因为您还需要从可绘制的文件夹中复制一些图像,您可以找到它your_sdk_drive: sdk platforms android-8

您可以从此处下载viewbadger.jar文件,可以使用此示例进行参考https://github.com/jgilfelt/android-viewbadger

这是示例代码

TabWidget Tabs =(TabWidget)findViewById(android.r.id.tabs);徽章徽章=新的badgeview(上下文,标签,3); badge.setTextsize(12); badge.setBadgePosition(badgeview.position_top_right); badge.setText(“ 0”)); badge.toggle();

我认为您有您的对象列表。您可以为对象创建属性“读取”,然后用 read = false 在打开标签的那一刻。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top