How to set ActionBar logo to Text (TextView)?

▼魔方 西西 提交于 2019-11-30 17:40:31

问题


I've been trying to make the ActionBar logo, or the Up button, a text instead of an image. actionbar.setLogo(); accepts only a drawable resource. Inflating a layout with a TextView didn't do the trick.

Basically, what I'm trying to accomplish is this:

The first one is from the new DeskClock app from Android 4.2 and the second is from the Contacts app. I checked the code in GitHub but I couldn't find anything.


回答1:


Here is an implementation of @CommonsWare's option #1, works perfectly.

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" />



回答2:


Off the cuff:

Option #1: Draw your text (TextView or directly) on a Bitmap-backed Canvas, and then supply a BitmapDrawable to setLogo().

Option #2: Hide the icon and logo and use setCustomView() to have your caret image and your text.



来源:https://stackoverflow.com/questions/14172654/how-to-set-actionbar-logo-to-text-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!