获取 Android Toolbar的真实准确高度问题

项目中设置了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后问题解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值