Android TextDrawable-getSupportActionBar.setLogo(可绘制可绘制)不起作用
我想把一个标志在我的工具栏与自定义绘图从 但是这个代码什么也不显示Android TextDrawable-getSupportActionBar.setLogo(可绘制可绘制)不起作用,android,android-layout,android-actionbar,toolbar,android-toolbar,Android,Android Layout,Android Actionbar,Toolbar,Android Toolbar,我想把一个标志在我的工具栏与自定义绘图从 但是这个代码什么也不显示 TextDrawable-drawable=TextDrawable.builder() .buildRound(“A”,颜色为红色); getSupportActionBar().setLogo(可绘制) 但如果我试着用“普通”的可拉丝布,它就行了 getSupportActionBar().setLogo(R.drawable.ic_launcher); 提前感谢编辑:在Mike M的评论中添加的解决方案。效果很好,但看起
TextDrawable-drawable=TextDrawable.builder()
.buildRound(“A”,颜色为红色);
getSupportActionBar().setLogo(可绘制)代码>
但如果我试着用“普通”的可拉丝布,它就行了
getSupportActionBar().setLogo(R.drawable.ic_launcher);
提前感谢编辑:在Mike M的评论中添加的解决方案。效果很好,但看起来很糟糕:
以下是此解决方案的代码:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextDrawable drawable = TextDrawable.builder().beginConfig().width(40).height(40).endConfig()
.buildRound("A", Color.RED);
toolbar.setLogo(drawable);
注意:在此解决方案中,您需要设置TextDrawable
徽标的width()
和height()
,因为它具有默认值-1
。否则,您将无法看到textdravable
图标
这是因为工具栏
类使用wrap\u内容
参数动态创建logo
TextDrawable
获取您的ImageView
的宽度和高度,因此请不要使用wrap\u content
值,否则它将获得默认的-1
值,您将看不到您的图像
而不是像下面的示例中那样设置硬编码值,match\u parent
或使用layout\u weight
设置所需的textdawable
大小
这是我的解决方案-使用自定义工具栏创建可文本绘制的徽标
使用名称action\u bar.xml创建自定义布局
把这个代码放进去
更改后,它应该如下所示:
希望能有所帮助我已经编辑了我的帖子。如您所见,您不需要使用beginConfig()
方法。只需设置图像视图的尺寸,并将其添加到自定义工具栏布局。它会给你更多的可能性和好看的外观:-)
<ImageView
android:id="@+id/image_view"
android:layout_width="32dp"
android:layout_height="32dp"
tools:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="@string/app_name"
android:textColor="#ffffff"
android:textSize="24sp"
android:textAlignment="gravity"
android:gravity="center_vertical"/>
//SET A DRAWABLE TO IMAGEVIEW
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_main);
TextDrawable drawable = TextDrawable.builder()
.buildRound("A", getResources().getColor(R.color.colorAccent));
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageDrawable(drawable);