项目中设置了Toolbar代码片段如下
<android.support.v7.widget.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="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center_vertical">
Toolbar里包含一个子view设置高度45dp
然后项目中需要在界面显示前获取Toolbar 的高度,动态设置其他view
但是代码获取的高度总是比实际高度小16px,Toolbar的高度也不是xml里设置的45dp,实际是56dp,检查了所有的代码没有找到设置的地方,于是用layout inspector 查看下了机器的实际界面:
发现Toolbar里还包含了一个ActionMenuView的子View,是Toolbar 用来显示Menu的里面的 Item 最小高度为56dp,源码如下:
/**
* ActionMenuView is a presentation of a series of menu options as a View. It provides
* several top level options as action buttons while spilling remaining options over as
* items in an overflow menu. This allows applications to present packs of actions inline with
* specific or repeating content.
*/
public class ActionMenuView extends LinearLayoutCompat implements MenuBuilder.ItemInvoker,
MenuView {
private static final String TAG = "ActionMenuView";
static final int MIN_CELL_SIZE = 56; // dips
加上最小56dp后问题解决