如何将视图放置在特定位置(x,y坐标)?

5
我在将我的视图放置到特定位置时遇到了问题(我的类继承自View)。我设计了一个将屏幕视为网格的游戏。我从用户那里获取不同类型视图的x和y坐标。
我的第一个任务是将正确的视图放置在其位置上。我正在使用RelativeLayout来设置屏幕。
注:我不想使用AbsoluteLayout参数,因为它已被截断。
2个回答

2

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

创建一个继承自你的父类ViewGroup的类,例如RelativeLayout或其他一些ViewGroup。 在重写onFinishInflate()方法中找到你自己的视图。

重写onLayout()方法,在调用super.onLayout()后手动布局你自己的视图。

最后,每次想要刷新你自己视图的位置时,只需调用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());
}

但这不是一种高效的解决方案。

如果你正在开发游戏,可以尝试使用 SurfaceView 或者甚至是 GLSurfaceView ,并通过 Canvas 自己绘制对象。


1
Suppose you want to place a button on a screen and you have divided the screen into 100x60 cells, with each cell measuring 8 pixels (800/100) along the horizontal axis and 8 pixels (480/60) along the vertical axis, considering that you are using a TAB with a resolution of 800x480 pixels. In landscape mode, divide the width by 100 and the height by 60, while in portrait mode, the division factor will be vice versa. Please note that HTML tags should be retained.

现在您想放置一个宽度为35个单元格,高度根据其上的文本自适应的按钮,并将其放置在坐标20,20(x,y)单元格编号。我已经提供了一种实现此目的的方法。请注意:无论您以这种方式或使用此想法放置的任何视图,在多个显示分辨率上都将处于相同的位置,并具有相同的尺寸和外观。

放置按钮的方法

public void placeButton(int btnId, int xCordinate, int yCordinate, int btnWidth,
        float fontSize, String btnTextColor, String btnBackgroundColor, String message){



    try{


        int currentApi = Build.VERSION.SDK_INT;
        /** Creating a new Button and setting its properties */
       /**pass context as a parameter for the constructor of the class if ur creating a new class just for placing views on screen,else it can be getBaseContext().*/
        Button btn = new Button(context);
        /**Use to access this view using Id to add any kind of listeners on this view.*/
        btn.setId(btnId);

        btn.setText(message);
        /**Let the text size be in pixels*/
        btn.setTextSize(TypedValue.COMPLEX_UNIT_PX, pixels_grid_X*fontSize);

        if(btnTextColor != null)

        {
            btn.setTextColor(Color.parseColor(btnTextColor));

        }

        int widthInPixel = (int) (btnWidth*pixels_grid_X);
        btn.setWidth(widthInPixel);


        if(btnBackgroundColor != null)
        {

            if(currentApi >= 16){
                btn.setBackground(new ColorDrawable(Color.parseColor(btnBackgroundColor )));
            }
            else
            {
                btn.setBackgroundDrawable(new ColorDrawable(Color.parseColor(btnBackgroundColor )));
            }
            /**Registering for a onTouch listener to provide clicked effect just like a button.
             * To provide a click effect for the custom button*/
            btn.setOnTouchListener(this);
        }
        else
        {   /**If btnBackgroundColor was equal to null, set default properties to that button.*/ 
            btn.setBackgroundResource(android.R.drawable.btn_default);
        }

        /** Providing layout params for the image view.*/
        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        params1.leftMargin = (int) (xCordinate*pixels_grid_X);
        params1.topMargin = (int) (yCordinate*pixels_grid_Y);
        btn.setLayoutParams(params1);

        /**
         * The parent layout in which the button is to be displayed.
         * Finally adding the button to the parent layout.
         * layout is the reference to ur relative layout.   
         */

        layout.addView(btn,params1);


    }catch(Exception ex){
        ex.printStackTrace();

    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接