[英]How can I use an icon instead of a title in the Android Toolbar?
I'm using the Toolbar (instead of ActionBar) via AppCompat.我通过 AppCompat 使用工具栏(而不是 ActionBar)。 I'd like to replace the Toolbar's title (the app/actity name) with an icon, but I don't see how.
我想用图标替换工具栏的标题(应用程序/活动名称),但我不知道该怎么做。
My icon is just text using a non-standard font, so I guess it might be allowed in Material Design.我的图标只是使用非标准字体的文本,所以我猜它可能在 Material Design 中被允许。
Use the following steps in your activity: 在您的活动中使用以下步骤:
Declare the Toolbar object: 声明工具栏对象:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool1);
Set the support actionbar: 设置支持操作栏:
setSupportActionBar(toolbar);
Remove title: 删除标题:
getSupportActionBar().setDisplayShowTitleEnabled(false);
Add your logo in drawable folder in: 在drawable文件夹中添加您的徽标:
res/drawable.
Add logo using this code: 使用此代码添加徽标:
toolbar.setLogo(R.drawable.ic_launcher);
At styles.xml first make a style for action bar with no title and with the logo like that在 styles.xml 首先为没有标题和标志的操作栏制作一个样式
<style name="ActionBar.NoTitle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="displayOptions">useLogo|showHome</item>
<item name="logo">@drawable/ic_logo</item>
</style>
Then create custome theme for your activity with new actionbar style然后使用新的操作栏样式为您的活动创建自定义主题
<!-- Create a style for MainActivity called AppTheme.Main that uses our custom ActionBar style -->
<style name="AppTheme.Main" parent="@style/AppTheme">
<item name="actionBarStyle">@style/ActionBar.NoTitle</item>
</style>
Then go to manifest file under the specified activity set theme to AppTheme.Main然后go到指定activity下的manifest文件设置主题为AppTheme.Main
android:theme="@style/AppTheme.Main"
I wish that help everyone希望对大家有所帮助
Here is the answer to a very similar question: Android Lollipop, add popup menu from title in toolbar 以下是一个非常相似的问题的答案: Android Lollipop,从工具栏中的标题添加弹出菜单
It comes down to using the Toolbar as a ViewGroup (LinearLayout is a ViewGroup too, for instance). 它归结为使用工具栏作为ViewGroup(例如,LinearLayout也是ViewGroup)。 The layout designer doesn't currently support that, but you can add a child node in the XML.
布局设计器当前不支持,但您可以在XML中添加子节点。
Then you need to call this to remove the standard title text: getSupportActionBar().setDisplayShowTitleEnabled(false); 然后你需要调用它来删除标准的标题文本:getSupportActionBar()。setDisplayShowTitleEnabled(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.