Android 工具栏样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26577528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Toolbar style
提问by Kenneth
I've added a Toolbarto my Lollipop project. However, I'm having trouble styling the Toolbar. My title in the Toolbarappears to have a bold font, but I'd like it to be italic. Any idea how to achieve this? I've tried playing around with the actionbar-style, no luck.
我在Toolbar我的 Lollipop 项目中添加了一个。但是,我在设计Toolbar. 我的标题Toolbar似乎有粗体,但我希望它是斜体。知道如何实现这一目标吗?我试过玩弄actionbar-style,没有运气。
I've set a style on the Toolbarwith app:theme="@style/toolbar"and my @style/toolbar(parent ThemeOverlay.AppCompat.ActionBar) is where I'm playing around with no good results.
我已经在Toolbarwith上设置了一个样式,app:theme="@style/toolbar"而我的@style/toolbar(父母ThemeOverlay.AppCompat.ActionBar)是我玩的地方,但没有什么好结果。
采纳答案by Dark Leonhart
You can add TextViewinside Toolbar, for example :
您可以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>
回答by jasonhudgins
This is actually really easy. There's a method on Toolbar called setTitleTextAppearance() that everyone seems to be overlooking. Just define your custom textAppearance in styles.xml, such as:
这实际上很容易。工具栏上有一个名为 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>
and then in your code, you just call :
然后在您的代码中,您只需调用:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextAppearance(this, R.style.MyTitleTextApperance);
and voila!
瞧!
回答by Binoy Babu
The ToolBar title is stylable. Any customization you make has to be made in the theme. I'll give you an example.
工具栏标题是可设计的。您所做的任何自定义都必须在主题中进行。我给你举个例子。
Toolbar layout:
工具栏布局:
<?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" />
Styles:
款式:
<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>
回答by Dynameyes
The problem mentioned above only occurs in Lollipop versions and not in Kitkat and lower versions. Lollipop does not follow the text style and put its own TitleTextAppearance. So even you add your own TextViewchild for your Toolbar(shown below), the android.support.v7.widget.Toolbarwill still override your styles.
上述问题仅在 Lollipop 版本中出现,在 Kitkat 及更低版本中不会出现。Lollipop 不遵循文字样式,而是放了自己的TitleTextAppearance. 因此,即使您TextView为您的Toolbar(如下所示)添加自己的孩子,它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>
To finally solve this, put your own TextViewfor the title and override the setTitlemethods of the Toolbarto skip the Lollipop's predefined TitleTextAppearance.
要最终解决此问题,请将您自己TextView的标题放在标题中并覆盖 的setTitle方法Toolbar以跳过 Lollipop 的预定义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);
}
}
回答by Aditya Vaidyam
You can apply a theme to the toolbar as follows:
您可以将主题应用到工具栏,如下所示:
<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" />
Just modify the style in your styles.xml, changing the TextAppearanceattribute, and you should be good to go.
只需修改styles.xml 中的样式,更改TextAppearance属性,就可以了。

