在Android中使用ActionBarSherlock时,动作条上的标签如何画?动作、标签、Android、ActionBarSherlock

2023-09-13 23:38:22 作者:刀魂使者

我使用 ActionBarSherlock 提供的其他动作pre蜂窝设备。

I am using ActionBarSherlock to provide ActionBars for pre HoneyComb devices.

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

My Activity has four fragments namely 1. User 2. Chat 3. Video 4. Extra, see image

我创建了动作条使用下面的code: -

I have created actionBar using following code:-

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

我要画的东西在这些选项卡,例如绘制和/或闪烁的聊天标签,每当聊天消息到达,并且用户在其他一些选项卡。

我怎样才能做到这一点?请帮忙。

How can I do this ? please help.

推荐答案

这是我如何解决我的问题,希望能对别人太有用了......

This is How I solved my problem, Hope it can be useful for someone else too....

首先,我通过扩展的ImageView创建CutomImageView

First I created a CutomImageView by extending ImageView

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

}

然后在创建选项卡,我用这个形象

Then While creating tabs, I used this image as

/ *设置自定义视图* /

/* Set Custom view */

    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方法​​通知更改显示的图像上。

Now whenever the notification changes I call increment/decrement or setter method of CustomImageView and new Notifications are displayed on the image..

建议,以改善这个解决方案是真正欢迎...

Suggestions to improve this solution are really welcome...