安卓:使用FEATURE_NO_TITLE定制的ViewGroup叶子在窗口的顶部空间 [英] Android: Using FEATURE_NO_TITLE with custom ViewGroup leaves space on top of the window

查看:252
本文介绍了安卓:使用FEATURE_NO_TITLE定制的ViewGroup叶子在窗口的顶部空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义的ViewGroup,我想用它采用了全屏幕应用程序。我现在用的是requestWindowFeature(Window.FEATURE_NO_TITLE),以隐藏标题栏。标题栏没有显示,但它仍然消耗在窗口的顶部空间。

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.

以上具有以下code生成的图像:

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);
        }
    }
}

完整的Eclipse项目是这里

这是有趣的,它是给这个空间我的自定义布局的Andr​​oid系统。我设置了CustomLayout作为我活动的根布局。在日志中的onLayout接收T = 25,这是什么推我的布局了。我不知道是我做错了,使机器人的T = 25(即标题栏完全相同的高度)。

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).

我运行此code在Android SDK 2.1,但我也发生在了Android 2.2。

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

修改:如果我改变了CustomLayout类的一些默认布局(如LinearLayout中),空间消失。当然,Android SDK中的默认布局不创建布局我想创建,所以这就是为什么我创造的。

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.

虽然我创建的布局有些复杂,这是最小的code我可以创建重现问题,我与我的布局。

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.

推荐答案

这不是一个完整的答案,但在此期间,你可以解决的问题,通过包装您的自定义布局在<$​​ C $ C>&LT;的FrameLayout / &GT;

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

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

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)

编辑:明白了。 onLayout()(和相应的布局()方法)指定的坐标不相对于屏幕的原点,它们是相对于亲本(<一href="http://developer.android.com/reference/android/view/View.html#layout%28int,%20int,%20int,%20int%29" rel="nofollow">http://developer.android.com/reference/android/view/View.html#layout%28int,%20int,%20int,%20int%29 )。因此,该系统是告诉你,你在相对坐标(0,38),和你传递下来给你的孩子,这意味着你是说,你的孩子是在屏幕坐标(0时,将其添加, 76),引起的间隙。

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.

你真正想要做的是:

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.

这篇关于安卓:使用FEATURE_NO_TITLE定制的ViewGroup叶子在窗口的顶部空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆