【问题标题】:Android Toolbar styleAndroid 工具栏样式
【发布时间】:2014-10-26 20:24:44
【问题描述】:

我已将Toolbar 添加到我的 Lollipop 项目中。但是,我无法为Toolbar 设置样式。我在Toolbar 中的标题似乎有一个粗体,但我希望它是斜体。知道如何实现这一目标吗?我试过玩actionbar-style,没有运气。

我在Toolbarapp:theme="@style/toolbar" 上设置了一个样式,而我的@style/toolbar(父ThemeOverlay.AppCompat.ActionBar)是我在玩但没有好的结果的地方。

【问题讨论】:

    标签: android styles android-5.0-lollipop android-toolbar


    【解决方案1】:

    这其实很简单。 Toolbar 上有一个名为 setTitleTextAppearance() 的方法,似乎每个人都忽略了它。只需在styles.xml中定义你自定义的textAppearance,如:

        <style name="MyTitleTextApperance" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textSize">20sp</item>
    </style>
    

    然后在您的代码中,您只需调用:

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextAppearance(this, R.style.MyTitleTextApperance);
    

    瞧!

    【讨论】:

    • 这不适用于问题中指定的textStyle(粗体、斜体)。
    【解决方案2】:

    工具栏标题是可设置样式的。您进行的任何自定义都必须在主题中进行。我给你举个例子。

    工具栏布局:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        style="@style/ToolBarStyle.Event"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="@dimen/abc_action_bar_default_height_material" />
    

    样式:

    <style name="ToolBarStyle" parent="ToolBarStyle.Base"/>
    
    <style name="ToolBarStyle.Base" parent="">
        <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
        <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
    </style>
    
    <style name="ToolBarStyle.Event" parent="ToolBarStyle">
        <item name="titleTextAppearance">@style/TextAppearance.Widget.Event.Toolbar.Title</item>
    </style>
    
    <style name="TextAppearance.Widget.Event.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
        <!--Any text styling can be done here-->
        <item name="android:textStyle">normal</item>
        <item name="android:textSize">@dimen/event_title_text_size</item>
    </style>
    

    【讨论】:

    • 你的工具栏布局在那里。是在它自己的名为toolbar.xml的文件中存储在values文件夹中......还是什么?你能指定这段代码的去向吗?
    • 谢谢。我花了很长的时间尝试不同的主题和样式变体,试图让我的工具栏文本为白色,“正常”字体粗细并有一个白色的后退按钮。对于这样一项微不足道的任务,这个解决方案似乎很冗长,但它是唯一对我有用的解决方案,所以谢谢。
    【解决方案3】:

    您可以在 Toolbar 中添加TextView,例如:

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/re/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" >
    
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="italic" />
    
    </android.support.v7.widget.Toolbar>
    

    【讨论】:

    • 酷。应该可以在无需添加自定义视图的情况下对其进行样式设置,你不觉得吗?
    • 您使用的是 Android Studio 吗?没有收到警告? “此处不允许使用元素 TextView”
    • @DarkLeonhart 我使用的是 Android Studio。
    • 我最终使用了这个,因为我还需要在标题上使用自定义字体。
    【解决方案4】:

    上述问题仅出现在 Lollipop 版本中,Kitkat 及更低版本中不会出现。 Lollipop 不遵循文字样式,放上自己的TitleTextAppearance。因此,即使您为您的Toolbar 添加自己的TextView 子代(如下所示),android.support.v7.widget.Toolbar 仍将覆盖您的样式。

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:theme="@style/someToolbarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/title"
            android:textAppearance="@style/TextAppearance.AppCompat.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </android.support.v7.widget.Toolbar>
    

    要最终解决这个问题,请将您自己的TextView 用作标题并覆盖ToolbarsetTitle 方法以跳过棒棒糖的预定义TitleTextAppearance

    public class YourCustomToolbar extends Toolbar {
    
        public YourCustomToolbar (Context context) {
            super(context);
        }
    
        public YourCustomToolbar (Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public YourCustomToolbar (Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
         }
    
        @Override
        public void setTitle(CharSequence title) {
            ((TextView) findViewById(R.id.title)).setText(title);
        }
    
        @Override
        public void setTitle(int title) {
            ((TextView) findViewById(R.id.title)).setText(title);
        }
    }
    

    【讨论】:

    • 请问您如何将 java 类映射到您的 XML 布局?您是否在所有构造函数中夸大了自定义布局?另外,您是否将布局包装在&lt;merge&gt; 中?我的最后一个问题是,您是通过将&lt;your.package. YourCustomToolbar/&gt; 放入活动的布局来使用它,还是使用&lt;include&gt;
    • 我编辑了您的帖子并将android:textAppearance="@style/TextAppearance.AppCompat.Title" 添加到TextView。这样,它的基本外观与正常的 AppBar 标题外观相同。
    【解决方案5】:

    您可以将主题应用到工具栏,如下所示:

    <android.support.v7.widget.Toolbar  
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="56dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    

    只需修改您的styles.xml 中的样式,更改TextAppearance 属性,就可以了。

    【讨论】:

    • 我已经尝试创建我在问题中所说的自定义样式,但没有运气。你能告诉我一个应该工作的示例样式吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多