Toolbar自定义布局
来源:互联网 发布:上海程序员培训机构 编辑:程序博客网 时间:2024/05/18 22:51
Toolbar如何使用想必大家清楚地很,实际开发中标题栏的样式各色各样,因此其基本样式便不能满足我们的需求,这就需要我们自定义布局。打开ToolBar源码我们发现它继承ViewGroup,这就表示我们可以把它当做一个存放控件的容器。
<android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"> <LinearLayout android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="标题" android:textSize="16sp" android:textColor="@color/white" /> </LinearLayout> </android.support.v7.widget.Toolbar>
结果:左边出现一片空白
查找源码发现:
final int contentInsetStart = a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetStart, RtlSpacingHelper.UNDEFINED); final int contentInsetEnd = a.getDimensionPixelOffset(R.styleable.Toolbar_contentInsetEnd, RtlSpacingHelper.UNDEFINED); final int contentInsetLeft = a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetLeft, 0); final int contentInsetRight = a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetRight, 0); mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight); if (contentInsetStart != RtlSpacingHelper.UNDEFINED || contentInsetEnd != RtlSpacingHelper.UNDEFINED) { mContentInsets.setRelative(contentInsetStart, contentInsetEnd); }
是这个contentInsetStart导致了左边的留白,我们只要将左边距置为0即可。
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:contentInsetLeft="0dp" app:contentInsetStart="0dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="标题" android:textSize="16sp" android:textColor="@color/white" /> </LinearLayout> </android.support.v7.widget.Toolbar>
这样Toobar的样式就可以任意由我们修改了
0 0
- Adnroid Toolbar 自定义Toolbar布局
- Android自定义ToolBar布局
- Toolbar自定义布局
- Toolbar自定义布局
- ToolBar之自定义布局
- Toolbar自定义样式,别致的布局,可复用
- Actionbar&Toolbar 自定义Menu 布局--ActionProvider的使用
- android使用5.0的toolbar,自定义布局参考地址
- 自定义toolbar布局不居中(已解决)
- 自定义toolbar布局不居中(已解决)
- 自定义toolbar
- 自定义toolbar
- 自定义ToolBar
- 自定义ToolBar
- 自定义ToolBar
- 自定义ToolBar
- 自定义Toolbar
- 自定义Toolbar
- poj 2778
- 计算矩阵边缘元素之和
- 时间复杂度和空间复杂度
- java服务器编程——log4j日志
- MySQL(十二)存储过程和函数的操作
- Toolbar自定义布局
- swift函数
- windows下桌面回收站图标的去除方法
- 系统拆分解耦利器之消息队列---RabbitMQ-主题(Topic)
- 图片懒加载解决方案 lazyload.js
- BroadcastReceiver基本用法
- 在本地安装Ik analyzer 兼容lucene 4.X版本
- Visual Stdio C++ 编译器 编译 (GSL) GNU Scientific Library 的方法介绍(3)
- Effective Modern C++ 条款20 把std::weak_ptr当作类似std::shared_ptr的、可空悬的指针使用