解决Toolbar的Icon、logo、title间距问题

来源:互联网 发布:妙笔生花绘画软件 编辑:程序博客网 时间:2024/05/18 06:19

1.修改title 边距

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

  xmlns:app="http://schemas.android.com/apk/res-auto"
  • 1
  • 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"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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


2.修改navigation icon的padding值

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

Widget.AppCompat.Toolbar.Button.Navigation
  • 1
  • 1

比如:

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
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

2.app主题中应用

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

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

@Overrideprotected void onTitleChanged(CharSequence title, int color) {    super.onTitleChanged(title, color);    if (toolbarTitle != null) {        toolbarTitle.setText(title);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

参考资料 - 点我


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
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

使用:

<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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

1 0
原创粉丝点击