【问题标题】:Badge on Android TabHostAndroid TabHost 上的徽章
【发布时间】:2011-11-28 07:33:34
【问题描述】:

我想实现与 iPhone 相同的功能

我在底部栏中实现了与 iPhone 中相同的自定义 Tabhost。我可以设置 正常/选定状态的两个图标,但我需要带有数量的动态图标 图片中给出的通知。

谢谢

【问题讨论】:

    标签: android android-tabhost android-custom-view android-tabactivity


    【解决方案1】:

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

    这是您可以通过此解决方案作为输出的快照:

    【讨论】:

    • 易于使用... BadgeViewer 是您将使用的类。您也可以复制而不是用作库..
    • @Paresh Mayani 我已经使用了这个 BadgeViewer 库,我在这里遇到了问题我已经发布了一个问题stackoverflow.com/questions/26099124/…
    【解决方案2】:

    这是如何在标签中添加徽章

    的示例

    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_indicator tab indicator.xml 是框架的标准可绘制选项卡,我从 sdk 获得,我建议你也应该得到它来自 sdk 中 api 的文件夹,因为您还需要从可绘制文件夹中复制一些图像,您可以找到它 your_sdk_drive:\sdk\platforms\android-8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2012-09-20
      • 2017-08-28
      相关资源
      最近更新 更多