简体   繁体   English

在标签栏android上显示通知

[英]show notification on tab bar android

I am developing an android app which has tabbar in it one tab is notification . 我正在开发一个Android应用程序,其中有一个tabbar是通知。 I have to display the number of notifications that are pending to that user i want to display the number of pending request is to be displayed on the icon. 我必须显示待该用户的待通知数量,我想显示待显示的待处理请求数量。

me having an activity of notification which shows number of notifications pending i need to put that count in tabbar.Icon of that tab is one square & i want to put digit to that square but i am not able to do that is there any notifiaction api which gives me that count also put in that any viewgroup. 我有一个通知活动,显示待通知的数量,我需要把这个计数放在tabbar.Icon的那个选项卡是一个方格我想把数字放到那个方块,但我不能这样做是否有任何notifiaction api这给了我那个计数也放入任何视图组。

thnx for any help............... thnx任何帮助...............

This is an example of How to add a badge in tab 这是如何在选项卡中添加徽章的示例

chat_tab.xml 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>

This is badge.xml (red circle for notifications background),TextView id:new_notifications background 这是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>

Then in the code you can simply do 然后在代码中你可以做到

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

As you can see my Relative Layout has a background @drawable/tab_indicator the tab indicator.xml is the framework's standard drawable of the tab,which i got from the sdk,i suggest you should also get it from the folder of the api in sdk as you also need to copy some images from the drawable folders,you can find it your_sdk_drive:\\sdk\\platforms\\android-8 你可以看到我的相对布局有一个背景@drawable/tab_indicator tab @drawable/tab_indicator标签的标准drawable,我从sdk获得,我建议你也应该从sdk中的api文件夹中获取它因为你还需要从drawable文件夹中复制一些图像,你可以找到它your_sdk_drive:\\ sdk \\ platforms \\ android-8

Another possibility, wich works for sure (I tried it): use TabSpec.setIndicator(View) . 另一种可能性,肯定是有效的(我试过):使用TabSpec.setIndicator(View) You can set whatever View you like for the Tab-Header. 您可以为Tab-Header设置您喜欢的任何View This should allow you to style the header however you want. 这应该允许您根据需要设置标题样式。

Try this example code: 试试这个示例代码:

public class TabTest extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TabHost tabHost = getTabHost(); // The activity TabHost
        TabHost.TabSpec spec; // Resusable TabSpec for each tab
        Intent intent; // Reusable Intent for each tab
        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, MyActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost
                .newTabSpec("artists").setIndicator(getLayoutInflater().inflate(R.layout.tablayout, null))
                .setContent(intent);
        tabHost.addTab(spec);
    }
}

and create a layout called tablayout . 并创建一个名为tablayout的布局。 Put whathever you like in this layout (I took a clock for a quick demo). 在这个布局中放置你喜欢的东西(我花了一个时钟进行快速演示)。 Also you have to create the MyActivity containing something that should be displayed when your tab is shown. 此外,您还必须创建包含在显示选项卡时应显示的内容的MyActivity

Screenshot: 截图:

Tab的屏幕截图

How about overriding the dispatchDraw method of the TabWidget ? 如何覆盖TabWidgetdispatchDraw方法? You can there use the drawing-api to draw the number onto the (already painted) tab-header. 您可以使用drawing-api将数字绘制到(已绘制的)标签页上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM