如何在Android中使用ActionBarSherlock时在ActionBar选项卡上绘制?

时间:2012-08-07 08:54:35

标签: android android-actionbar actionbarsherlock android-ui

我正在使用ActionBarSherlock为前HoneyComb设备提供ActionBars。

我的活动有四个片段,即1.用户2.聊天3.视频4.额外,见图像

enter image description here

我使用以下代码创建了actionBar: -

            actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.setTitle("Meeting");
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowCustomEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);

        /* Set Custom view */


        ActionBar.Tab tab = actionBar.newTab();
        // tab.setText("Meeting Users");
        tab.setIcon(R.drawable.users);
        tab.setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab();
        // tab.setText("Chat");
        tab.setIcon(R.drawable.chat);
        tab.setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab();
        // tab.setText("Video");
        tab.setIcon(R.drawable.video_call);
        tab.setTabListener(this);
        tab.select();
        actionBar.addTab(tab);

        tab = actionBar.newTab();
        // tab.setText("Extra");
        tab.setIcon(R.drawable.extra);
        tab.setTabListener(this);
        actionBar.addTab(tab);

我想在这些标签上绘制一些内容,例如,当聊天消息到达且用户在其他标签上时,在聊天标签上绘制和/或闪烁。

我该怎么做?请帮忙。

2 个答案:

答案 0 :(得分:3)

使用标签的自定义视图

  ActionBar.Tab tab = getSupportActionBar().newTab();
  tab.setCustomView(R.layout.custom_tab_view);

然后,您可以获得有关自定义布局的视图并进行闪烁

答案 1 :(得分:1)

这就是我如何解决我的问题,希望它对其他人也有用....

首先,我通过扩展ImageView

创建了一个CutomImageView
package com.myexample.newsessionwindowsrbrdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.renderscript.Font.Style;
import android.widget.ImageView;

public class CustomImageView extends ImageView {
    private int notificationCount;

    /**
     * @param context
     */
    public CustomImageView(Context context) {
        super(context);
        notificationCount = 0;
    }

    public synchronized void incrementNotification() {
        notificationCount--;
        this.invalidate();
    }

    public synchronized void decrementNotification() {
        notificationCount++;
        this.invalidate();
    }

    /**
     * @return the notificationCount
     */
    public synchronized int getNotificationCount() {
        return notificationCount;
    }

    /**
     * @param notificationCount
     *            the notificationCount to set
     */
    public synchronized void setNotificationCount(int notificationCount) {
        this.notificationCount = notificationCount;
        this.invalidate();
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.ImageView#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        System.out.println("OnDraw is called");
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        paint.setFakeBoldText(true);
        paint.setTextSize(15);
        canvas.drawText(String.valueOf(notificationCount), 15, 20, paint);
    }

}

然后在创建标签时,我将此图像用作

/ *设置自定义视图* /

    mView = new CustomImageView(this);
    mView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));

    mView.setBackgroundResource(R.drawable.users);
    ActionBar.Tab tab = actionBar.newTab();
    tab.setCustomView(mView);
    tab.setTabListener(this);
    actionBar.addTab(tab);

现在每当通知发生变化时,我都会调用CustomImageView的增量/减量或setter方法,并在图像上显示新的通知。

enter image description here

非常欢迎提出改进此解决方案的建议......