Android:将 FEATURE_NO_TITLE 与自定义 ViewGroup 一起使用会在窗口顶部留下空间

发布于 2024-12-19 11:40:39 字数 1854 浏览 1 评论 0原文

我正在尝试创建一个自定义 ViewGroup,并且我想将其与全屏应用程序一起使用。我正在使用“requestWindowFeature(Window.FEATURE_NO_TITLE)”来隐藏标题栏。标题栏没有显示,但它仍然占用窗口顶部的空间。

这是问题的图片

上面的图像是使用以下代码生成的:

public class CustomLayoutTestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Button b = new Button(this);
        b.setText("Hello");
        CustomLayout layout = new CustomLayout(this);
        layout.addView(b);
        setContentView(layout);
    }
}

public class CustomLayout extends ViewGroup {
    public CustomLayout(Context context) {
        super(context);
    }
    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.i("CustomLayout", "changed="+changed+" l="+l+" t="+t+" r="+r+" b="+b);
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; ++i) {
            final View v = getChildAt(i);
            v.layout(l, t, r, b);
        }
    }
}

(完整的 Eclipse 项目在这里

有趣的是,Android 为我的自定义布局提供了这个空间。我将 CustomLayout 设置为 Activity 的根布局。在日志中的“onLayout”正在接收“t=25”,这就是推动我的布局的原因。我不知道我做错了什么,导致 Android 变成“t=25”(这正是标题栏的高度)。

我在 Android SDK 2.1 中运行此代码,但也在 Android 2.2 中运行。


编辑:如果我更改某些默认布局(例如 LinearLayout)的 CustomLayout 类,空间就会消失。当然,Android SDK 的默认布局不会创建我想要创建的布局,因此这就是我创建一个布局的原因。

尽管我正在创建的布局有些复杂,但这是我可以创建的最小代码,可以重现我的布局问题。

I am trying to create a custom ViewGroup, and I want to use it with a full screen application. I am using the "requestWindowFeature(Window.FEATURE_NO_TITLE)" to hide the title bar. The title bar is not showing, but it still consuming space on top of the window.

Here is a picture of the problem

The image above was generated with the following code:

public class CustomLayoutTestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Button b = new Button(this);
        b.setText("Hello");
        CustomLayout layout = new CustomLayout(this);
        layout.addView(b);
        setContentView(layout);
    }
}

public class CustomLayout extends ViewGroup {
    public CustomLayout(Context context) {
        super(context);
    }
    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.i("CustomLayout", "changed="+changed+" l="+l+" t="+t+" r="+r+" b="+b);
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; ++i) {
            final View v = getChildAt(i);
            v.layout(l, t, r, b);
        }
    }
}

(The full Eclipse project is here)

It is interesting to see that it is the Android that is given this space for my custom layout. I am setting the CustomLayout as the root layout of my Activity. In the Log in the "onLayout" is receiving "t=25", and that is what is pushing my layout down. What I don't know is what I am doing wrong that makes Android the "t=25" (which is exactly the height of the title bar).

I am running this code in the Android SDK 2.1, but I also happens in Android 2.2.


EDIT: If I change the CustomLayout class for some default layout (such as LinearLayout), the space disappears. Of course, the default layouts of Android SDK don't create the layout I am trying to create, so that is why I am creating one.

Although the layout I am creating is somewhat complex, this is the smallest code I could create reproducing the problem I have with my layout.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香橙ぽ 2024-12-26 11:40:39

这不是一个完整的答案,但与此同时,您可以通过将自定义布局包装在 中来解决该问题

此外,值得注意的是,您的布局超出了框架的底部屏幕。它向下移动了标题栏高度(在我的模拟器中为 38 像素)

编辑:明白了。 onLayout() (以及相应的layout() 方法)指定坐标不相对于屏幕原点,它们相对于父级( http://developer.android.com/reference/android/view/View.html#layout%28int,%20int,%20int,%20int%29 )。因此,系统告诉您您位于相对坐标 (0, 38),并且您在将其传递给您的孩子时添加它,这意味着您说您的孩子位于屏幕坐标 (0, 76),造成差距。

您真正想要做的是:

v.layout(0, 0, r - l, b - t);

这将使您的子视图与视图的左上角对齐,并与视图具有相同的宽度和高度。

It's not a full answer, but in the meantime you can work around the problem by wrapping your custom layout in a <FrameLayout />

Also, it's worth noting that your layout extends beyond the bottom of the screen. It's shifted down by the title bar height (38 pixels in my emulator)

Edit: Got it. onLayout() (and the corresponding layout() method) specify that the coordinate are not relative to the screen origin, they're relative to the parent ( http://developer.android.com/reference/android/view/View.html#layout%28int,%20int,%20int,%20int%29 ). So the system is telling you that you're at relative coordinates (0, 38), and you're adding it when passing that down to your child, which means that you're saying that your child is at screen coordinates (0, 76), causing the gap.

What you actually want to do is:

v.layout(0, 0, r - l, b - t);

That will put your child Views aligned with the top left corner of your View, with the same width and height as your view.

微凉 2024-12-26 11:40:39

我在 2.2 中的 FrameLayout 遇到了同样的问题,

我通过将 android:layout_gravity="top" 添加到 FrameLayout 来修复它

I had the same issue with a FrameLayout in 2.2

I fixed it by adding android:layout_gravity="top" to the FrameLayout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文