Java 如何将ActionBar徽标设置为文本(TextView)?
我一直在尝试将ActionBar徽标或Up按钮制作成文本而不是图像<代码>actionbar.setLogo()代码>仅接受可绘制资源。用Java 如何将ActionBar徽标设置为文本(TextView)?,java,android,android-actionbar,graphical-logo,Java,Android,Android Actionbar,Graphical Logo,我一直在尝试将ActionBar徽标或Up按钮制作成文本而不是图像actionbar.setLogo()仅接受可绘制资源。用TextView来膨胀版面并没有起到作用 基本上,我要做的是: 第一个来自Android 4.2的新DeskClock应用程序,第二个来自Contacts应用程序。我在GitHub中检查了代码,但找不到任何东西。即时: 选项1:在位图背景的画布上绘制文本(TextView或直接绘制),然后向setLogo()提供一个BitmapDrawable 选项2:隐藏图标和徽标,并
TextView
来膨胀版面并没有起到作用
基本上,我要做的是:
第一个来自Android 4.2的新DeskClock应用程序,第二个来自Contacts应用程序。我在GitHub中检查了代码,但找不到任何东西。即时:
选项1:在位图
背景的画布
上绘制文本(TextView
或直接绘制),然后向setLogo()
提供一个BitmapDrawable
选项2:隐藏图标和徽标,并使用
setCustomView()
显示插入符号图像和文本。这里是@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\u 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的缺点是无法显示标题,因为标题位于自定义视图的左侧。但是选项#1是完美的,请看我的答案。@matthias:欢迎您的自定义视图包含标题
TextView
,以及插入符号和其他文本。话虽如此,我很高兴你有了令你满意的工作。