猿问

状态栏的高度?

有没有一种方法可以获取状态栏和标题栏的高度?检查开发人员论坛会显示相同的问题,但没有解决方案(我可以找到)。

我知道我们可以在初始布局通过后获得它,但是我希望在我的活动的onCreate()中获得它。

谢谢


一只甜甜圈
浏览 660回答 3
3回答

catspeake

获取状态栏高度的支持方法是使用WindowInsets从API 21+开始的类:customView.setOnApplyWindowInsetsListener((view, insets) -> {    // Handle insets    return insets.consumeSystemWindowInsets();});或WindowInsetsCompat用于支持库:ViewCompat.setOnApplyWindowInsetsListener(customView, (view, insets) -> {    // Handle insets    return insets.consumeSystemWindowInsets();});您还可以onApplyWindowInsets在视图内部覆盖方法:public class CustomView extends View {    @Override    public WindowInsets onApplyWindowInsets(final WindowInsets insets) {        final int statusBarHeight = insets.getStableInsetTop();        return insets.consumeStableInsets();    }}有关更多详细信息,我建议检查Chris Banes的演讲- 成为主窗口装配工(此处提供幻灯片)。
随时随地看视频慕课网APP

相关分类

Android
我要回答