1.修改title 边距

修改边距使用系统的app属性来引入使用,即:

  xmlns:app="http://schemas.android.com/apk/res-auto"
  • 1

比如:

<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:orientation="vertical"
app:contentInsetLeft="10dp"/>

当然也可以在style.xml中修改,自己研究吧;


2.修改navigation icon的padding值

修改padding值,就需要在style.xml中修改;在此我们修改的是navigation的pading值:

Widget.AppCompat.Toolbar.Button.Navigation

比如:

1.定义style

    <style name="myToolbarNavigationButtonStyle" parent="@style/Widget.AppCompat.Toolbar.Button.Navigation">
<item name="android:minWidth">0dp</item>
<item name="android:padding">@dimen/margin_horizontal_large</item>
<item name="android:scaleType">centerInside</item>
</style>
  • 1

2.app主题中应用

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="toolbarNavigationButtonStyle">@style/myToolbarNavigationButtonStyle</item>
</style>
  • 1

3.title居中

toolbar是可以自定义布局的,可以在toolbar中添加一个textview来实现,从而代替title;
比如:

1.布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
​ android:layout_height="?actionBarSize"
android:background="@null"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/AppTheme"> <TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/> </android.support.v7.widget.Toolbar>

2.初始化

View v = findViewById(R.id.toolbar);
if (v != null) {
toolbar = (Toolbar) v;
setSupportActionBar(toolbar);
toolbarTitle = (TextView) v.findViewById(R.id.toolbar_title);
if (toolbarTitle != null) {
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}

3.使用
设置title两种方式:
(1)直接在setText();
(2)在AndroidManifest.xml中指定title;
(3)如果有baseActivity的话,解决如下:
Activity有一个onTitleChanged的接口,在Activity的onPostCreate与setTitle中都会调用这个方法;

protected void onPostCreate(@Nullable Bundle savedInstanceState) {
if (!isChild()) {
mTitleReady = true;
onTitleChanged(getTitle(), getTitleColor());
}
mCalled = true;
}

所以只需要在BaseActivity中重载这个方法就行了,如下所示 :

@Override
protected void onTitleChanged(CharSequence title, int color) {
super.onTitleChanged(title, color);
if (toolbarTitle != null) {
toolbarTitle.setText(title);
}
}

参考资料 - 点我


4.修改menu的padding值

定义style值:

    <style name="mToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
<item name="android:paddingRight">0dp</item>
<item name="android:paddingLeft">13dp</item>
</style>
  • 1

使用:

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:orientation="vertical"
style="@style/mToolbarStyle"/>
  • 1

上面的方法,在有些机子上会失效,大家有上面办法,请告诉我,谢谢!


5.参考

修改toolbar的一些属性
toolbar学习使用

Android - toolbar navigation 样式的更多相关文章

  1. Android Toolbar样式定制详解

    前言 Marterial Design出来也有一段时间了,为了紧跟Google的设计规范,决定在项目中使用Toolbar.使用了一段时间之后,发现很多时候原始的Toolbar并不能满足项目的要求.为了 ...

  2. Android ToolBar

    众所周知,在使用ActionBar的时候,一堆的问题:这个文字能不能定制,位置能不能改变,图标的间距怎么控制神马的,由此暴露出了ActionBar设计的不灵活.为此官方提供了ToolBar,并且提供了 ...

  3. [置顶] xamarin android toolbar(踩坑完全入门详解)

    网上关于toolbar的教程有很多,很多新手,在使用toolbar的时候踩坑实在太多了,不好好总结一下,实在浪费.如果你想学习toolbar,你肯定会去去搜索androd toolbar,既然你能看到 ...

  4. Android - Bottom Navigation View

    目录 Android - Bottom Navigation View Android - Bottom Navigation View Overview 一直以来,关于Android的底部导航的功能 ...

  5. ANDROID – TOOLBAR STEP BY STEP(转)

    今年(2014) 的 Google I/O 發表令多數人為之一亮的 Material Design,而 Google 也從「Google I/O 2014」 開始,大家也陸陸續續地看到其更新的 And ...

  6. Android RatingBar 自定义样式

    Android RatingBar 自定义样式 1.先定义Style: <style name="RadingStyle" parent="@android:sty ...

  7. android中的样式和主题

    有的时候我们一个页面要用很多个textview,而且这些textview的样式非常相像,这种情况下我们可以把这些样式抽取出来,然后在每个textview中引用即可,这样修改起来也方便. 我们来看一个简 ...

  8. xamarin android checkbox自定义样式

    xamarin android checkbox自定义样式 在drawable文件在新建checkbox_bg.xml文件 <?xml version="1.0" encod ...

  9. Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式

    Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式 Fragment FragmentManager frag ...

随机推荐

  1. abp ef codefirst 设置默认值

    public partial class tableIsWaringfiled : DbMigration { public override void Up() { //设置默认值为true Add ...

  2. 第八章 Internet控制报文协议

    Internet控制报文协议 首先,我们必须先清楚,IP协议本身没有为终端系统提供直接的方法来发现那些发往目的地址失败的IP数据包,并且IP没有提供直接的方式来获取诊断信息,那么我们的故事来了. In ...

  3. weex 开发 (已放弃了)

    关于weex 开发 本菜已放弃使用了,当初选择使用weex 是为了同时支持h5 和 android / ios  三端:想法很不错,深入之后 突然发现,开发起来并没有很轻松,因为weex 中有些方法, ...

  4. codeforces school mark(贪心)

    ///太渣,看了题解才知道怎么做,自己想感觉想不清楚 ///题解:首先在给出的序列里判断小于median的个数,若大于(n-1)/2,则不满足,否则看另一个条件 ///这样我们可以把中位数左边还要添加 ...

  5. 牛客 NOIp模拟1 T1 中位数 解题报告

    中位数 题目描述 小\(N\)得到了一个非常神奇的序列\(A\).这个序列长度为\(N\),下标从\(1\)开始.\(A\)的一个子区间对应一个序列,可以由数对\([l,r]\)表示,代表\(A[l] ...

  6. 【CZY选讲·一道图论神题】

    题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,只有点权. LYK想把这个图删干净,它的方法是这样的.每次选择一个点,将它删掉,但删这个点是需要代价的 ...

  7. bzoj4772 显而易见的数论

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=4772 sol :这个题卡了我一整天QAQ 出题人简直丧心病狂,卡内存+卡常数QAQ 题意就是, ...

  8. Java 文件hashCode

    public static void main(String args[]) { try { System.out.println(getMD5Checksum("RationalRoseE ...

  9. HDU2824 The Euler function

    Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Description The Eule ...

  10. V4L2驱动的移植与应用(二+三)【转】

    转自:http://blog.chinaunix.net/uid-10747583-id-298489.html 原文地址:http://blog.csdn.net/wxzking/archive/2 ...