android toolbar教程,Android Toolbar style

I've added a Toolbar to my Lollipop project. However, I'm having trouble styling the Toolbar. My title in the Toolbar appears 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.

I've set a style on the Toolbar with app:theme="@style/toolbar" and my @style/toolbar (parent ThemeOverlay.AppCompat.ActionBar) is where I'm playing around with no good results.

You can add TextView inside Toolbar, for example :

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" >

android:id="@+id/title"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textStyle="italic" />

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:

@android:color/white

20sp

and then in your code, you just call :

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

toolbar.setTitleTextAppearance(this, R.style.MyTitleTextApperance);

and voila!

The ToolBar title is stylable. Any customization you make has to be made in the theme. I'll give you an example.

Toolbar layout:

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/ThemeOverlay.AppCompat.Light

@style/ThemeOverlay.AppCompat.Dark.ActionBar

@style/TextAppearance.Widget.Event.Toolbar.Title

normal

@dimen/event_title_text_size

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 TextView child for your Toolbar (shown below), the android.support.v7.widget.Toolbar will still override your styles.

android:id="@+id/toolbar"

android:theme="@style/someToolbarStyle"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/title"

android:textAppearance="@style/TextAppearance.AppCompat.Title"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

To finally solve this, put your own TextView for the title and override the setTitle methods of the Toolbar to skip the Lollipop's predefined 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);

}

}

You can apply a theme to the toolbar as follows:

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 TextAppearance attribute, and you should be good to go.

来源:https://stackoverflow.com/questions/26577528/android-toolbar-style

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值