如何在ActionBar中动态更改图标的通知计数

时间:2015-11-25 12:05:21

标签: android

1.遗赠您的徽章菜单项:       actionbar_badge_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res
        /android"
      android:layout_width="48dp"
      android:layout_height="fill_parent"
      android:layout_gravity="right" >

<!-- Menu Item Image -->
<ImageView
    android:layout_width="48dp"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:src="@drawable/Notificationicon" />

<!-- Badge Count -->    
<TextView
    android:id="@+id/actionbar_notifcation_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@drawable/badge_circle"

    />

2.main.xml文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/badge"
    android:actionLayout="@layout/actionbar_badge_layout"
   android:title="Notification"
    android:showAsAction="always">
</item>
</menu>

3.Within MainActivity:

   public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         inflater.inflate(R.menu.badge, menu);
         RelativeLayout badgeLayout = (RelativeLayout)    
             menu.findItem(R.id.badge).getActionView();
         TextView tv = (TextView)  
     badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
         tv.setText("10");
    }

4.Emulator OutPut:

 TextView(badge) is not setting to 10, in Emulator blank textview is  showing.

5.现在我使用ActionBar和滑动菜单。

1 个答案:

答案 0 :(得分:1)

I did a Small mistake With updating badge.Finally I got Updating  
badge in Actionbar with Sliding menu.

The code:

1.BaseActivity :(icon onclick effect and updating)


 private int number = 2;
  private TextView tv = null;

   @Override
  public boolean onCreateOptionsMenu(final Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main, menu);
    final View menu_list = 
          menu.findItem(R.id.menu_list).getActionView();
    tv = (TextView) menu_list.findViewById(R.id.list);
    updateHotCount(number);
    new MyMenuItemStuffListener(menu_list, "Show  message") {
        @Override
        public void onClick(View v) {
            System.out.println("icon Onclick Effet");
           }
        };
        return super.onCreateOptionsMenu(menu);
      }

     // call the updating code on the main thread,
    // so we can call this asynchronously
   public void updateHotCount(final int new_number) {
     number = new_number;
     if (tv == null) return;
     runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (new_number == 0)
                tv.setVisibility(View.INVISIBLE);
            else {
                tv.setVisibility(View.VISIBLE);
                tv.setText(Integer.toString(new_number));
              }
           }
       });
     }

  static abstract class MyMenuItemStuffListener implements 
   View.OnClickListener, View.OnLongClickListener {
     private String hint;
     private View view;

    MyMenuItemStuffListener(View view, String hint) {
        this.view = view;
        this.hint = hint;
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
    }


    @Override 
   abstract public void onClick(View v);

    @Override
   public boolean onLongClick(View v) {
        final int[] screenPos = new int[2];
        final Rect displayFrame = new Rect();
        view.getLocationOnScreen(screenPos);
        view.getWindowVisibleDisplayFrame(displayFrame);
        final Context context = view.getContext();
        final int width = view.getWidth();
        final int height = view.getHeight();
        final int midy = screenPos[1] + height / 2;
        final int screenWidth =  
        context.getResources().getDisplayMetrics().widthPixels;
        Toast cheatSheet = Toast.makeText(context, hint, 
          Toast.LENGTH_SHORT);
        if (midy < displayFrame.height()) {
            cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT,
                    screenWidth - screenPos[0] - width / 2, height);
        } else {
            cheatSheet.setGravity(Gravity.BOTTOM | 
              Gravity.CENTER_HORIZONTAL, 0, height);
        }
        cheatSheet.show();
        return true;
      }
   }


 2.Drawable/badge_circle.xml:

 <?xml version="1.0" encoding="utf-8"?>

<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">

  <solid android:color="#ffffff" />
  <stroke android:color="#ffffff" android:width="4dp"/>
</shape>

3.action_bar_notifitcation_icon.xml :


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res  
     /android"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:gravity="center"
 android:layout_gravity="center"
 android:clickable="true"
 style="@android:style/Widget.ActionButton">

<ImageView
    android:id="@+id/list_bell"
    android:src="@drawable/ic_action_mail"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:gravity="center"
    android:layout_margin="0dp"
    android:contentDescription="bell"
    />

<TextView 
    android:id="@+id/list"
    android:layout_width="wrap_content"
    android:minWidth="17sp"
    android:textSize="12sp"
    android:textColor="#000000"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@null"
    android:layout_alignTop="@id/list_bell"
    android:layout_alignRight="@id/list_bell"
    android:layout_marginRight="0dp"
    android:layout_marginTop="3dp"
    android:paddingBottom="1dp"
    android:paddingRight="4dp"
    android:paddingLeft="4dp"
    android:background="@drawable/badge_circle"/>
 </RelativeLayout>

4.menu/Main.xml :

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >

  <item android:id="@+id/menu_list"
    android:actionLayout="@layout/action_bar_notifitcation_icon"
    android:showAsAction="always"
    android:icon="@drawable/ic_action_mail"
    android:title="list" />

 </menu>
5.Emulator Output :
    [![Sliding menu][1]][1]
    [onclick sliding menuitem][2],
    [icon onclick effect][3]