有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将ActionBar徽标设置为文本(TextView)?

我一直在尝试将ActionBar徽标或Up按钮制作成文本而不是图像actionbar.setLogo();只接受可绘制资源。用TextView膨胀布局并没有奏效

基本上,我要做的是:

enter image description here

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


共 (2) 个答案

  1. # 1 楼答案

    即兴:

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

    选项#2:隐藏图标和徽标,并使用setCustomView()显示插入符号图像和文本

  2. # 2 楼答案

    这里是@commonware选项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" />