【问题标题】:How can I use an icon instead of a title in the Android Toolbar?如何在 Android 工具栏中使用图标而不是标题?
【发布时间】:2015-01-03 05:36:34
【问题描述】:

我通过 AppCompat 使用工具栏(而不是操作栏)。我想用图标替换工具栏的标题(应用程序/活动名称),但我不知道如何。

我的图标只是使用非标准字体的文本,所以我想它可能在 Material Design 中是允许的。

【问题讨论】:

    标签: android material-design android-toolbar


    【解决方案1】:

    在您的活动中使用以下步骤:

    1. 声明工具栏对象:

      Toolbar toolbar = (Toolbar) findViewById(R.id.tool1);
      
    2. 设置支持操作栏:

      setSupportActionBar(toolbar);   
      
    3. 删除标题:

      getSupportActionBar().setDisplayShowTitleEnabled(false);
      
    4. 将您的徽标添加到可绘制文件夹中:

      res/drawable.
      
    5. 使用此代码添加徽标:

      toolbar.setLogo(R.drawable.ic_launcher);
      

    【讨论】:

    • 如何设置logo尺寸/边距?
    • @fnc12 "?attr/actionBarSize" 怎么样?
    • 但是如果我想添加另一个徽标怎么办?例如,左侧的实际徽标和中心的图像(实际上是图像按钮)。
    【解决方案2】:

    这是一个非常相似的问题的答案: Android Lollipop, add popup menu from title in toolbar

    归结为将工具栏用作 ViewGroup(例如,LinearLayout 也是一个 ViewGroup)。布局设计器目前不支持,但您可以在 XML 中添加子节点。

    然后您需要调用它来删除标准标题文本: getSupportActionBar().setDisplayShowTitleEnabled(false);

    【讨论】:

    • 其实kunal.c的回答要好很多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2019-12-22
    • 2018-01-01
    相关资源
    最近更新 更多