简体   繁体   中英

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 code 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?

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.

使用AbsoluteLayout (灵活性较差,难以维护。)并指定确切的位置(x和y)。

Use with layout_x, layout_y to set initial position and set to update position. 与layout_x,layout_y一起使用以设置初始位置,并设置以更新位置。


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

您可以使用offsetLeftAndRightoffsetTopAndBottom方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM