Android(安卓)- toolbar 优化 title修改边距和navigation icon修改padding值
16lz
2021-01-24
1.修改title 边距
修改边距使用系统的app属性来引入使用,即:
xmlns:app="http://schemas.android.com/apk/res-auto"
比如:
<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">0dpitem> <item name="android:padding">@dimen/margin_horizontal_large - "android:scaleType"
>centerInside style>
2.app主题中应用
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="toolbarNavigationButtonStyle">@style/myToolbarNavigationButtonStyle style>
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中重载这个方法就行了,如下所示 :
@Overrideprotected void onTitleChanged(CharSequence title, int color) { super.onTitleChanged(title, color); if (toolbarTitle != null) { toolbarTitle.setText(title); }}
参考资料 - 点我
4.修改menu的padding值
定义style值:
使用:
.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"/>
上面的方法,在有些机子上会失效,大家有上面办法,请告诉我,谢谢!
5.参考
修改toolbar的一些属性
toolbar学习使用
更多相关文章
- Android――Android(安卓)lint工具项目资源清理详解
- Android(安卓)程序完全退出的几种方法(参考)
- 给android添加系统属性:Settings.system和SystemProperties
- ViewDragHelper与Transition的示例代码
- android上拔出sd卡导致flash上媒体也消失问题解决方法
- Android(安卓)Studio调试设置的断点无效
- android:gravity="bottom|center_horizontal"的妙用
- android中的网络解析xml,json,html框架
- Android的消息处理机制(从源码分析)