如何将视图放置在特定位置(x,y坐标)
我在将视图(myclass扩展视图)放置在特定位置时遇到问题。 我设计了一个将屏幕视为网格的游戏。我从用户的特定视图的x和y坐标中获得(具有不同类型的视图)。如何将视图放置在特定位置(x,y坐标)
我的第一个任务是设置正确的视图。我该怎么做。 我为屏幕使用了relativelayout。
p.s.我不想使用绝对布局参数,因为它被截断。
我很乐意为您提供一个示例或解释正确的方法。
由于提前, Amigal
这不是实现这一目标的最佳途径,但它是一个解决方案...
创建一个类继承父ViewGroup中。说RelativeLayout或其他一些ViewGroups。 在重写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自行绘制对象。
假设你想在屏幕上放置一个按钮,并且在你的网格中有100x60单元格。通过考虑沿水平和垂直方向的像素数量,您已将屏幕分为100x60像元。在横向模式下,宽度像素的数量会更多,因此可以考虑将其除以100,并且高度像素的数量将会减少60个。因此,考虑到使用TAB的TAB具有分辨率为800x480像素0127,风景模式 - 你有每个单元8个像素(800/100)沿着水平线和沿垂直方向每个单元8个像素(480/60)。纵向模式反之亦然。
现在ü要放置宽度的按钮说35细胞和高度根据在其上的文本WRAP_CONTENT并将其放置在坐标20,20(X,Y)细胞数。我已经提供了一种方法来实现这一点。并注意:以这种方式或使用这种想法的任何人都会看到你将会在同一个地方,并且在多个显示分辨率上具有相同的尺寸和外观。[这里pixels_grid_X =每单元像素沿着水平(800/100)] [Similiarly pixels_grid_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();
}
}
方法