我在將視圖(myclass擴展視圖)放置在特定位置時遇到問題。 我設計了一個將屏幕視爲網格的遊戲。我從用戶的特定視圖的x和y座標中獲得(具有不同類型的視圖)。如何將視圖放置在特定位置(x,y座標)
我的第一個任務是設置正確的視圖。我該怎麼做。 我爲屏幕使用了relativelayout。
p.s.我不想使用絕對佈局參數,因爲它被截斷。
我很樂意爲您提供一個示例或解釋正確的方法。
由於提前, Amigal
我在將視圖(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();
}
}
方法