如何将视图放置在特定位置(x、y 坐标) [英] How to place views in a specific location (x, y coordinates)

查看:32
本文介绍了如何将视图放置在特定位置(x、y 坐标)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将视图(我的类扩展 View)放置在特定位置时遇到问题.我设计了一个将屏幕视为网格网的游戏.我从特定视图的用户 x 和 y 坐标处获得(我有不同类型的视图).

I have a problem with placing my views (my class extends View) in a specific location. I have designed a game that treats the screen as a net of grids. I get from the user x and y coordinates of a specific view (I have different types of views).

我的第一个任务是设置正确的视图.我该怎么做?我在屏幕上使用 RelativeLayout.

My first mission is to set the correct view in its place. How can I do it? I am using RelativeLayout for the screen.

附言我不想使用 AbsoluteLayout 参数,因为它被截断了.

P.S. I don't want to use AbsoluteLayout params because it was truncated.

推荐答案

这不是实现它的最佳方式,但它是一个解决方案...

This is not the best way to achieve it, but it's a solution...

创建一个继承父 ViewGroup 的类,比如 RelativeLayout 或其他一些 ViewGroup.在覆盖 onFinishInflate() 中找到您自己的视图.

Create a class that extends your parent ViewGroup, say RelativeLayout or some other ViewGroups. Find your own view in overriding onFinishInflate().

在调用super.onLayout()后覆盖onLayout()方法并手动布局你自己的视图.

Override the onLayout() method and manually layout your own view after calling super.onLayout().

最后,每次想要刷新自己视图的位置时,只需调用requestLayout()即可.

Finally, every time you want to refresh the location of your own view, just call requestLayout().

以下是示例:

private AwesomeView mMyView;
private int mYourDesiredX;
private int mYourDesiredY;

@Override
public void onFinishInflate() {
    super.onFinishInflate();
    if (mMyView == null) {
        mMyView = (AwesomeView)findViewById();
    }
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    mMyView.layout(mYourDesiredX,
            mYourDesiredY,
            mYourDesiredX + mMyView.getWidth(),
            mYourDesiredY + mMyView.getHeight());
}

但这不是一个有效的解决方案.

But this is not an efficient solution.

如果您正在制作游戏,请尝试SurfaceView 甚至GLSurfaceView 并使用Canvas 自己绘制对象.

If you are making games, try SurfaceView or even GLSurfaceView and draw the objects by your own with Canvas.

这篇关于如何将视图放置在特定位置(x、y 坐标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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