在安卓中使用ActionBarSherlock时如何在ActionBar标签上绘制?

3

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

我的Activity有四个片段,分别是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);

我希望在这些标签上绘制一些东西,例如在聊天选项卡上绘制或闪烁,每当有聊天消息到达且用户正在其他选项卡上时。
我该怎么做?请帮忙。

1
https://developer.android.com/training/basics/actionbar/styling.html - The Demz
2个回答

3

使用自定义视图来管理您的选项卡

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

然后你可以在自定义布局上获得视图,让其闪烁。

如果我添加自定义视图,图标就会消失...请问如何解决这个问题,请帮忙。 - Amit
感谢提供代码...我知道那个会起作用,但是我想在图片上绘制文本,比如Facebook通知...或者类似于SO通知也可以...我已经尝试过扩展ImageView并使用它,但结果不如预期。看来我必须要用Paint/Canvas来实现了...有什么建议吗? - Amit
看一下这里:https://dev59.com/f3A75IYBdhLWcg3wPmZ8 或许会有帮助。 - Georgy Gobozov

1

这是我解决问题的方法,希望对其他人也有用...

首先,我通过扩展ImageView创建了一个CustomImageView。

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的增量/减量或设置器方法,并在图像上显示新的通知。

enter image description here

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接