Android:新しいアイテムの数を表示するタブでカウントされます

StackOverflow https://stackoverflow.com/questions/4343418

  •  30-09-2019
  •  | 
  •  

質問

ホーム画面がタブビューであるAndroidアプリケーションがあります。タブの1つは、「受信トレイ」アクティビティに移動します。新しいメッセージの数を示す番号を表示したいと思います。これは、iPhoneアプリの標準機能のようです。これに対するシステムサポートはありますか?そうでない場合、Android Tabviewsにこの効果を実装する最も簡単な方法は何ですか?

役に立ちましたか?

解決

Android Sourcode(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)しかし、tabSpecが[http://developer.android.com/reference/android/widget/tabhost.tabspec.html#setindicator(java.lang.charescence、android.graphics.drawable.drawable)]で初期化されていることを確認してください。 1]。あなたはそこにあなた自身の描画可能を渡すことができ、それを使って自分でバッジをペイントすることができます。

iPhoneバッジはラベルの上部の角に非常にうまくいっているため、iPhoneスタイルのバッジには代わりはありません。そしてこれまでのところ、それを変更する方法はありません。

一方、これはすべてJavaであり、誰かがTabhostクラスを拡張し、iPhoneスタイルのバッジのサポートを追加できます。

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

他のヒント

これはの例です タブにバッジを追加する方法

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 タブ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);バッジビューバッジ=新しいバッジビュー(コンテキスト、タブ、3); badge.settextsize(12); Badge.setBadgePosition(BadgeView.Position_Top_right); badge.settext( "0")); badge.toggle();

私はあなたがあなたのオブジェクトのリストを持っていると思います。オブジェクトのプロパティ「読み取り」を作成してから、オブジェクトの数をでカウントできます。 read = false タブを開く瞬間。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top