Question

J'ai une application android où l'écran d'accueil est une vue de l'onglet. L'un des onglet va à une activité « Boîte de réception ». Je voudrais montrer un nombre qui indique le nombre de nouveaux messages. Cela semble être une caractéristique standard sur les applications iPhone. Y at-il sorte de soutien du système pour cela? Sinon, quelle est la meilleure façon de mettre en œuvre cet effet pour android tabviews?

Était-ce utile?

La solution

D'après ce que je peux lire dans l'Android Sourcecode (TabHost.java) il n'y a pas moyen de le faire avec des widgets standards d'une manière similaire comme l'iPhone.

Ce que vous pouvez faire est:

  1. Chaque fois que le nombre de boîte de réception a été changé, supprimez tous les onglets en utilisant http: / /developer.android.com/reference/android/widget/TabHost.html#clearAllTabs de ()
  2. Ensuite, créez les onglets en utilisant à nouveau http://developer.android.com/reference/android/widget/TabHost.html#addTab (android.widget.TabHost.TabSpec ), mais assurez-vous que le TabSpec est initialisé avec [http: // développeur. android.com/reference/android/widget/TabHost.TabSpec.html#setIndicator(java.lang.CharSequence, android.graphics.drawable.Drawable)] [1]. Vous pouvez passer votre drawable là-bas et l'utiliser pour peindre le vous badge.

Il n'y a pas de remplacement pour le badge de style iPhone mais, comme le badge iPhone est tout à fait bien dans les coins de topRight de l'étiquette. Et jusqu'à présent, il n'y a pas moyen de le changer.

Par contre, ce qui est tout quelqu'un Java pourrait étendre la classe TabHost et ajouter le support pour les badges de style iPhone.

[1]: http: //developer.android.com/reference/android/widget/TabHost.TabSpec.html#setIndicator(java.lang.CharSequence , android.graphics.drawable.Drawable)

Autres conseils

Ceci est un exemple de Comment ajouter un badge dans l'onglet

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 (cercle rouge pour les notifications arrière-plan), id TextView: background 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>

Ensuite, dans le code que vous pouvez simplement faire

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

Comme vous pouvez le voir ma disposition relative a un @drawable/tab_indicator de fond l'onglet indicator.xml est drawable standard du cadre de l'onglet, que je suis arrivé du sdk, je suggère que vous devriez aussi obtenir de le dossier de l'api dans sdk que vous avez également besoin de copier des images à partir des dossiers étirables, vous pouvez le trouver your_sdk_drive: \ sdk \ plateformes \ android-8

Vous pouvez télécharger le fichier viewbadger.jar d'ici et peut utiliser cet exemple pour référence https://github.com/jgilfelt/android-viewbadger

Voici un exemple de code

TabWidget languettes = (TabWidget) findViewById (android.R.id.tabs); BadgeView insigne = new BadgeView (contexte, onglets, 3); badge.setTextSize (12); badge.setBadgePosition (BadgeView.POSITION_TOP_RIGHT); badge.setText ( "0")); badge.toggle ();

Je suppose que vous avez une liste de vos objets. Vous pouvez créer une propriété « lire » pour l'objet, puis de compter le nombre d'objets avec read = false au moment de l'ouverture de l'onglet.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top