首页 > 解决方案 > 如何在 Android XML 布局中获取状态栏高度?

问题描述

我有两个相同的屏幕,屏幕中央有一个徽标。

但是,一个屏幕考虑了状态栏高度,而另一个没有考虑,因此徽标的位置在两者之间不匹配。

这是xml上的代码。

屏幕 1

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/blue" />
    <item
        android:height="200dp"
        android:width="200dp"
        android:gravity="center"
        android:drawable="@mipmap/ic_launcher_foreground">
    </item>
</layer-list>

屏幕 2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue"
    android:gravity="center">

    <ImageView
        android:layout_height="200dp"
        android:layout_width="200dp"
        android:layout_marginBottom = "39dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher_foreground"/>

</LinearLayout>

我使用galaxy s10,它的状态栏高度为39dp。现在我可以手动设置 39 的边距以匹配徽标的位置(如上面的代码),但这仅适用于一个特定的设备(即 s10)。我需要为不同的移动设备动态设置边距。

我该怎么做呢?为什么一个屏幕集成了状态栏高度而另一个没有?

Ps 这是一个 react-native 项目。

标签: androidreact-nativesplash-screen

解决方案


推荐阅读