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

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

问题描述

我有把我的意见(MyClass的扩展视图)在一个特定位置时出现问题。 我设计了一个游戏,把屏幕作为一个净网格。予从用户x和特定视图的y坐标取(有不同类型的视图)。

I have a problem with placing my views (myclass extends view) in a specific location. I have designed a game that treat the screen as a net of grids. I get from the user x and y coordinates of a specific view (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.

P.S。我不想用absolutelayout PARAMS,因为它已被截断。

p.s. I don't want to use absolutelayout params because it was truncated.

我会很乐意得到一个例子,或在正确的方式作出解释,以做到这一点。

I will be happy to get an example or an explanation on the correct way to do it.

在此先感谢, Amigal

Thanks in advance, Amigal

推荐答案

这是不是实现这一目标的最佳途径,但它是一个解决方案...

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

创建一个类继承你的父母的ViewGroup。说RelativeLayout的或其他一些ViewGroups。 找到你自己的覆盖onFinishInflate()查看。

create a class extends your parent viewgroup. say RelativeLayout or some other ViewGroups. find your own view in overriding onFinishInflate().

覆盖onLayout()方法,并调用超onLayout(后手动布局自己的观点)。

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

下面是下面的示例...

here is a sample below...

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 a efficient solution.

如果你ř制作游戏,尝试SurfaceView甚至GLSurfaceView并通过你自己的帆布画的对象。

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

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

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