簡體   English   中英

在標簽欄android上顯示通知

[英]show notification on tab bar android

我正在開發一個Android應用程序,其中有一個tabbar是通知。 我必須顯示待該用戶的待通知數量,我想顯示待顯示的待處理請求數量。

我有一個通知活動,顯示待通知的數量,我需要把這個計數放在tabbar.Icon的那個選項卡是一個方格我想把數字放到那個方塊,但我不能這樣做是否有任何notifiaction api這給了我那個計數也放入任何視圖組。

thnx任何幫助...............

這是如何在選項卡中添加徽章的示例

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 @drawable/tab_indicator標簽的標准drawable,我從sdk獲得,我建議你也應該從sdk中的api文件夾中獲取它因為你還需要從drawable文件夾中復制一些圖像,你可以找到它your_sdk_drive:\\ sdk \\ platforms \\ android-8

另一種可能性,肯定是有效的(我試過):使用TabSpec.setIndicator(View) 您可以為Tab-Header設置您喜歡的任何View 這應該允許您根據需要設置標題樣式。

試試這個示例代碼:

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

並創建一個名為tablayout的布局。 在這個布局中放置你喜歡的東西(我花了一個時鍾進行快速演示)。 此外,您還必須創建包含在顯示選項卡時應顯示的內容的MyActivity

截圖:

Tab的屏幕截圖

如何覆蓋TabWidgetdispatchDraw方法? 您可以使用drawing-api將數字繪制到(已繪制的)標簽頁上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM