修改边距使用系统的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"/> 12345678910 12345678910当然也可以在style.xml中修改,自己研究吧;
修改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> 12345 123452.app主题中应用
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="toolbarNavigationButtonStyle">@style/myToolbarNavigationButtonStyle</item> </style> 123 123toolbar是可以自定义布局的,可以在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> 12345678910111213141516171819 123456789101112131415161718192.初始化
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); } } 123456789 1234567893.使用 设置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; } 1234567 1234567所以只需要在BaseActivity中重载这个方法就行了,如下所示 :
@Override protected void onTitleChanged(CharSequence title, int color) { super.onTitleChanged(title, color); if (toolbarTitle != null) { toolbarTitle.setText(title); } } 1234567 1234567参考资料 - 点我
定义style值:
<style name="mToolbarStyle" parent="@style/Widget.AppCompat.Toolbar"> <item name="android:paddingRight">0dp</item> <item name="android:paddingLeft">13dp</item> </style> 1234 1234使用:
<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"/> 123456789 123456789上面的方法,在有些机子上会失效,大家有上面办法,请告诉我,谢谢!