TextDrawable - getSupportActionBar.setLogo(Drawable drawable) 无效
TextDrawable - getSupportActionBar.setLogo(Drawable drawable) not works
我想在我的工具栏中放置一个带有来自 https://github.com/amulyakhare/TextDrawable 的自定义可绘制对象的徽标。
但是这段代码什么也没显示
TextDrawable drawable = TextDrawable.builder()
.buildRound("A", Color.RED);
getSupportActionBar().setLogo(drawable);
但如果我尝试使用 "normal" 可绘制对象,它会起作用。
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);
NOTE: In this solution you need to set width()
and height()
of your TextDrawable
logo, as it has as default value -1
. Without that you won't see your TextDrawable
icon.
That's because of that the Toolbar
class creates logo
dynamically with wrap_content
parameters.
TextDrawable
takes width and height of your ImageView
, so please DON'T use wrap_content
value, otherwise it would get default -1
value and you won't see your image.
Instead of it, set hard-coded value like in an example below, match_parent
or use layout_weight
to set how big your TextDrawable
you want.
这是我的解决方案 - 使用自定义工具栏创建一个 TextDrawable 徽标
- 创建名称为
action_bar.xml
的自定义布局
输入这段代码
<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"/>
添加到 MainActivity
class 中的 onCreate
方法,此代码:
//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);
更改后应如下所示:
希望对您有所帮助
我想在我的工具栏中放置一个带有来自 https://github.com/amulyakhare/TextDrawable 的自定义可绘制对象的徽标。
但是这段代码什么也没显示
TextDrawable drawable = TextDrawable.builder()
.buildRound("A", Color.RED);
getSupportActionBar().setLogo(drawable);
但如果我尝试使用 "normal" 可绘制对象,它会起作用。
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);
NOTE: In this solution you need to set
width()
andheight()
of yourTextDrawable
logo, as it has as default value-1
. Without that you won't see yourTextDrawable
icon.That's because of that the
Toolbar
class createslogo
dynamically withwrap_content
parameters.
TextDrawable
takes width and height of yourImageView
, so please DON'T usewrap_content
value, otherwise it would get default-1
value and you won't see your image.Instead of it, set hard-coded value like in an example below,
match_parent
or uselayout_weight
to set how big yourTextDrawable
you want.
这是我的解决方案 - 使用自定义工具栏创建一个 TextDrawable 徽标
- 创建名称为
action_bar.xml
的自定义布局
输入这段代码
<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"/>
添加到
MainActivity
class 中的onCreate
方法,此代码://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);
更改后应如下所示:
希望对您有所帮助