public class Game_collecting_view extends View
{
Button image_boy;
private static final int BOY_DIAMETER = 200; // initial spot size
int boy_width =0;
int boy_height =0;
public void setGame_collecting(Game_collecting mGame_collecting)
{
this.mGame_collecting = mGame_collecting;
}
// constructs a new View
public Game_collecting_view(Context context, RelativeLayout parentLayout)
{
super(context);
resources = context.getResources(); // save Resources for loading external values
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// get references to various GUI components
relativeLayout = parentLayout;
spotHandler = new Handler(); // used to add spots when game starts
}
@Override
protected void onSizeChanged(int width, int height, int oldw, int oldh)
{
viewWidth = width; // save the new width
viewHeight = height; // save the new height
}
public void set_boy()
{
final Button boy = (Button) layoutInflater.inflate(R.layout.untouched, null);
boy.setX(viewWidth /2);
boy.setY(viewHeight - BOY_DIAMETER);
boy.setPadding(0,0,0,0);
boy.setBackgroundResource(R.drawable.blue);
boy.setLayoutParams(new RelativeLayout.LayoutParams(BOY_DIAMETER, BOY_DIAMETER));
relativeLayout.addView(boy); // add spot to the screen
Toast.makeText(getContext(), "set_boy\nviewWidth=" +viewHeight +"\nviewHeight=" +viewHeight, Toast.LENGTH_SHORT).show();
boy.setOnClickListener
(
new OnClickListener()
{
public void onClick(View v)
{
touchedSpot(boy);
}
}
);
}
public void resume(Context context)
{
resetGame();
}
public void resetGame()
{
for (int i = 1; i <= INITIAL_SPOTS; ++i)
{
spotHandler.postDelayed(addSpotRunnable, i * SPOT_DELAY);
generate_text();
}
set_boy();
}
private Runnable addSpotRunnable = new Runnable()
{
public void run()
{
addNewSpot(); // add a new spot to the game
}
};
目的:
我想在屏幕的底部中间设置男孩图标。 男孩图标被设置在这样的后动态接口(轻扫屏幕男孩图标将移动到的权利,反之亦然)安卓:设置以编程方式使用setX的和SETY按钮
观察:
的敬酒报告同时viewWidth和viewHeight = 0,男孩图标出现在0,0(左上角)。如果我设置了setY(viewHeight + BOY_DIAMETER),男孩图标将位于(0,200)。
问:
我想问一下,为什么viewWidth和viewHeight都报告0怎能onSizeChanged
被称为立即使得男孩ICONE可以在屏幕的底部中心设置?
谢谢!
http://stackoverflow.com/questions/19932253/get-positions-of-views-in-a-dialog-relativ-to-their-parent/ 19933242#19933242请在这里查看我的答案 – Naddy
在元素布置在屏幕上之前,您要求的尺寸太早。 – njzk2
@ njzk2:正确的你是对的,我现在使用'getViewTreeObserver()'在通过'setX'和'setY'之前派生'ViewWidth'和'ViewHeight'。但男孩的图标仍然位于(0,0)。我试过如果使用'setX(720/2)'和'setY(1134-200)'它可能位于所需的位置。这意味着在设置男孩图标之前还没有收到'viewWidth'和'viewHeight' ....这样我怎么能推迟定位男孩图标呢?使用'Handler'我怎么知道维度何时被检索? – pearmak