获取状态栏的高度,标题栏的高度,文本栏的高度

本文介绍了如何在Android中获取状态栏、标题栏和内容区域的高度。通过设置系统UI可见性监听器并操作 DecorView,可以获取到各个部分的高度,以便进行界面布局调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<span style="font-size:18px;">在Android中Window对象通常由PhoneWindow来实现,PhoneWindow将一个DecorView设置为整改应用窗口的根View</span>
<span style="font-size:18px;">final View decorView = getActivity().getWindow().getDecorView();</span>
<span style="font-size:18px;">设置了系统UI可见性改变监听器
decorView.setOnSystemUiVisibilityChangeListener(
<span style="white-space:pre">	</span>new View.OnSystemUiVisibilityChangeListener() {
<span style="white-space:pre">	</span>@Override
        <span style="white-space:pre">	</span>public void onSystemUiVisibilityChange(int i) {
                <span style="white-space:pre">	</span>int height = decorView.getHeight();
                        Log.i(TAG, "Current height: " + height);
                }
});</span>
<span style="font-size:18px;"></span><pre name="code" class="java"><span style="font-size:18px;">//</span><span style="font-family: Arial, Helvetica, sans-serif;">得到当前的UI标记</span>
int uiOptions = getActivity().getWindow().getDecorView().getSystemUiVisibility();int newUiOptions = uiOptions;

<span style="font-size:18px;"></span><pre name="code" class="java"><span style="font-size:18px;">//</span><span style="font-family: Arial, Helvetica, sans-serif;">判断是否为沉浸模式</span>
boolean isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);// Immersive mode: Backward compatible to KitKat (API 19).// Note that this flag doesn't do anything by itself, it only augments the behavior// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample// all three flags are being toggled together.// This sample uses the "sticky" form of immersive mode, which will let the user swipe// the bars back in again, but will automatically make them disappear a few seconds later.

<span style="font-size:18px;">newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;</span>
<span style="font-size:18px;">getActivity().getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
</span>
<span style="font-size:18px;"></span><pre name="code" class="java">//得到的容器包含状态栏和decor_content_parent
ViewGroup group = (ViewGroup) getWindow().getDecorView();
<pre name="code" class="java"><span style="font-size:18px;">//顶部状态栏</span>
View statubar = group.getChildAt(1);if (statubar != null) { int statuHeight = statubar.getMeasuredHeight(); Log.i(TAG, "statuHeight height: " + statuHeight);}

<pre name="code" class="java"><span style="font-size:18px;">//id为content的内容view</span>
ViewGroup child = (ViewGroup) group.getChildAt(0);View contentView = child.getChildAt(0);if (contentView != null) { int contentHeight = contentView.getMeasuredHeight(); Log.i(TAG, "contentHeight height: " + contentHeight);}

<pre name="code" class="java"><span style="font-size:18px;">//actionbar</span>
View titleView = child.getChildAt(1);if (titleView != null) { int titleHeight = titleView.getMeasuredHeight(); Log.i(TAG, "titleView height: " + titleHeight);}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值