如何在android--statusBar上添加字符显示

最近修改statusBar,需要在上面添加自定义的字符。
版本:android-2.3.1
--------------------------------------------------

1.先参考statusbar上时间的布局文件:
frameworks\base\packages\SystemUI\res\layout\status_bar.xml

 <LinearLayout android:id="@+id/statusIcons"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:paddingRight="6dip"
            android:gravity="center_vertical"
            android:orientation="horizontal"/>    

        <com.android.systemui.statusbar.Clock  
            android:textAppearance="@*android:style/TextAppearance.StatusBar.Icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:singleLine="true"
            android:paddingRight="6dip"
            android:gravity="center_vertical|left"
            />
        <com.android.systemui.statusbar.HandSetNum  //我们所需要显示的字符
			android:textAppearance="@*android:style/TextAppearance.StatusBar.Icon"
			android:layout_width="wrap_content"
			android:layout_height="match_parent"
			android:textSize="14sp"
			android:gravity="center_vertical"
			android:paddingLeft="10dip"
			android:paddingRight="10dip"
			/>



注释: <com.android.systemui.statusbar.Clock 
       <com.android.systemui.statusbar.HandSetNum 
意思很明确,就是在statusbar目录下添加一个类名文件,我们参考closk,在
\frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\下创建一个HandSetNum.java 文件,里面内容基本跟clock.java一样。
android:gravity="center_vertical" :意思就是在组件里面的位置
android:layout_gravity="center_vertical" :指组件在layout的位置。

android:paddingLeft="10dip"  :离组件边距
android:paddingRight="10dip"

2.编写HandSetNum.java


public class HandSetNum extends TextView {
    private boolean mAttached;
	
    public HandSetNum(Context context) {
        this(context, null);

    }

    public HandSetNum(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HandSetNum(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        if (!mAttached) {
            mAttached = true;
            IntentFilter filter = new IntentFilter();

            filter.addAction(Intent.ACTION_HANDSET_NUM);
            getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
        }
   
        updateHandSetNum();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mAttached) {
            getContext().unregisterReceiver(mIntentReceiver);
            mAttached = false;
        }
    }

    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_HANDSET_NUM)) {
				updateHandSetNum();
            }
            
        }
    };

    final void updateHandSetNum() {

        	setText("Hs:1");
    }
}//end HandSetNum




基本跟clock.java代码相同,主要以setText();来实现字符的显示。
3.编译android源码

你可能感兴趣的:(android)