Android:为什么 ContentView.getTop() 中存在差异取决于 FEATURE_NO_TITLE?

标签 android layout statusbar titlebar custom-titlebar

在我看来,Android 的坐标系统存在问题。当我有一个普通 View (不请求 FEATURE_NO_TITLE)时,我然后检索 int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();。这给了我 76px:38px 用于状态栏,38px 用于标题栏。

但是,如果我请求 FEATURE_NO_TITLE 然后重复该过程,getTop() 返回 0px,尽管事实上状态栏仍然可见!

这种差异应该不会有什么不同,因为我们通常不关心内容 View 从哪里开始。但是,它对我来说确实很重要,因为我将 View 放置在装饰 View 上——它覆盖了整个可见窗口。

我知道这不是标题栏和设备密度的技巧,因为如果我请求自定义标题栏,并给它 0 高度,然后 getTop() 返回 38px

解决方案/解决方法是在请求 FEATURE_NO_TITLE 时手动添加 38 像素。我的问题是:这是一个 Android 错误吗? 或者是否有一些我不了解布局如何使这种行为可以理解的东西?

提前致谢!

这是重现问题的最小程序。运行它两次,并取消注释指示的行。我正在针对 Android SDK 7 进行编译,并在装有 Android 2.3.4 的三星 Galaxy S 上运行。

布局:main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutParent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/someId"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </LinearLayout>

</RelativeLayout>

代码:TestStatusBarActivity.java

package com.test.teststatusbar;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.RelativeLayout;

public class TestStatusBarActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Uncomment the following line to see the alternate behavior
        //requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);

        RelativeLayout layoutParent = (RelativeLayout) findViewById(R.id.layoutParent);
        View something = findViewById(R.id.someId);
        something.setBackgroundColor(Color.CYAN);

        ViewTreeObserver vto = layoutParent.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                // decor window top
                Rect rectgle = new Rect();
                Window window = getWindow();
                window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
                int StatusBarHeight = rectgle.top;

                // "content view" top
                int contentViewTop = window.findViewById(
                        Window.ID_ANDROID_CONTENT).getTop();

                int TitleBarHeight = contentViewTop - StatusBarHeight;

                Log.i("STATUSBARTEST", "StatusBar Height = " + StatusBarHeight
                        + " , TitleBar Height = " + TitleBarHeight
                        + ", Content top = " + contentViewTop);
            }
        });

    }
}

引用资料

最佳答案

is this an Android bug?

没有。系统只是构建 View 层次结构,在两种情况下都略有不同,避免不必要的深度(出于性能原因,更多(嵌套)布局元素意味着更多开销)

下面是带有标题栏的层次结构:

注意:我的像素值与您的略有不同,因为我在较小的模拟器上运行了您的样本。对于这种情况,这无关紧要。

enter image description here full size

包含标题栏和内容布局的基本 LinearLayout 会填满整个显示。它在这里也有一个 25px 的填充,因为状态栏覆盖了正常的 UI 层次结构。所以必须通过填充保留一些空间。

然后是标题栏,作为 LinearLayout 的第一个元素,高度也为 25 px。这意味着 findViewById(Window.ID_ANDROID_CONTENT).getTop(); 总共返回 50,因为 contentview FrameLayout 距离其父 LinearLayout 顶部 50 像素 (同样是 25 padding + 25 title )。这是正确的。

那么,当标题栏被移除时会发生什么?

enter image description here full size

系统完全去除了外层的 LinearLayout,这意味着内容 View 现在填满了整个屏幕。 findViewById(Window.ID_ANDROID_CONTENT).getTop(); 应该在此处返回 0,它实际上做了什么。这没有错。但是又要为状态栏预留空间。系统在此处将该填充分配给内容 View 。

如何修复

我认为解决方案非常简单:您必须包含内容 View 的填充以获得准确的结果。例如。改变

int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();

View contentView = window.findViewById(Window.ID_ANDROID_CONTENT);
int contentViewTop = contentView.getTop() + contentView.getPaddingTop();

这会为我打印出正确的结果。

关于Android:为什么 ContentView.getTop() 中存在差异取决于 FEATURE_NO_TITLE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10734309/

相关文章:

android - 如何检查 wifi 或 gprs 中的互联网

android - 如何下载Android应用程序的sqlite数据库?

android - 改变方向后 ImageView 消失

xml - 为什么这个基于XML布局的Android应用程序崩溃?

android - 从 Photoshop android 导入没有背景的 Photoshop 形状(按钮)

xcode - didenterbackground 后如何更改状态栏?

Cocoa:状态栏中的 NSPopover 无响应

android - Titanium Appcelerator 安卓模块 : How to create a proxy?

android - 当文本超过一行时换行 TextView

android - 使用全屏隐藏 android 4.4+ 或 kitkat 中的状态栏