13

我想实现的功能一样在那里在iPhone徽章在Android TabHost

enter image description here

我实现的定制Tabhost在底栏相同的iPhone。我可以为Normal/Selected状态设置两个图标 ,但我需要动态图标,其数量为 ,如图中所示。

感谢

回答

18

Android ViewBadger可能是您的解决方案。 (仅供参考,我还没有实现它)

这里是你可以通过这个解决方案的输出卡:

enter image description here

+0

好用... BadgeViewer是,你会使用的类。你也可以复制,而不是使用它作为库.. –

+0

@Paresh Mayani我已经使用这个BadgeViewer库,我面临的问题在这里我发布了一个问题http://stackoverflow.com/questions/26099124/tabhost-set-在徽章位置在选项卡-Android –

3

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

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(红圈的通知背景),TextView 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_indicatortab indicator.xml是该标签的框架的标准绘图,我从SDK获得,我建议你也应该从api的文件夹中获取它在SDK中还需要一些图片来自提拉文件夹复制,你可以找到它 your_sdk_drive:\ SDK \平台\ Android的8

+1

thnks raja babar :) –