如何将ActionBar徽标设置为Text(TextView)?

时间:2013-01-05 14:20:00

标签: java android android-actionbar graphical-logo

我一直试图将ActionBar徽标或“向上”按钮设为文本而不是图像。 actionbar.setLogo();仅接受可绘制资源。使用TextView对布局进行充气并不起作用。

基本上,我想要完成的是:

enter image description here

第一个来自Android 4.2的新DeskClock应用程序,第二个来自Contacts应用程序。我检查了GitHub中的代码但我找不到任何东西。

2 个答案:

答案 0 :(得分:5)

脱下袖口:

选项#1:在TextView支持的Bitmap上绘制文字(Canvas或直接),然后向BitmapDrawable提供setLogo()

选项#2:隐藏图标和徽标,并使用setCustomView()获取您的插入图片和文字。

答案 1 :(得分:3)

这是@ CommonsWare选项#1的实现,效果很好。

TextView upTextView = (TextView) getLayoutInflater().inflate(
        R.layout.up_text, null);
upTextView.setText("CITIES");
upTextView.measure(0, 0);
upTextView.layout(0, 0, upTextView.getMeasuredWidth(), 
        upTextView.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(upTextView.getMeasuredWidth(),
        upTextView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
upTextView.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);

R.layout.up_text

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:textColor="#f00"
    android:textSize="20sp"
    android:textStyle="bold" />
相关问题