开发者

Setting button location through Java Code?

I'm thinking of making a small android game like Mole Catch but if i want the mole to jump around randomly on the screen as a button i would need to make the button change location in the java cod开发者_运维技巧e and not in the XML and im not realy sure how i would change this location.. Like if i create the button in xml and then the buttons position will still be set in the xml file not in java.

So is there some way i can instead of setting position of button in XML then set the position in Java for this kind of game / application?


You could use the method offsetLeftAndRight and offsetTopAndBottom


Hi TheComppBoy in your case my suggestion is don't create your button in xml file. Make it dynamic and add it into the any layout(possibly in relative layout which is define in xml file) .

and as your mole is jump change the button gravity as required.

it may help you.


Use AbsoluteLayout (It is less flexible and harder to maintain.) and specify exact location (x and y).


Use AbsoluteLayout with layout_x, layout_y to set initial position and set layoutParams to update position.


public AbsoluteLayout.LayoutParams (int width, int height, int x, int y)

Creates a new set of layout parameters with the specified width, height and location. Parameters

width   the width, either MATCH_PARENT, WRAP_CONTENT or a fixed size in pixels
height  the height, either MATCH_PARENT, WRAP_CONTENT or a fixed size in pixels
x   the X location of the child
y   the Y location of the child
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜