开发者

how to set a button or imagebutton in a view created by our own and its position also

is it possible to set a button or imagebutton inside a view created by our own and to set position that button or imagebutton.

public class TestingGraphics extends Activity {
    /** Called when the activity is first created. */
   RelativeLayout layout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        layout = new RelativeLayout(this);
        layout.setId(R.layout.main);
        setContentView(layout);
        Button button = new Button(this);
        /*-----------开发者_运维知识库-----------------------*/
    }


You can do that with ViewGroup.addView(View child, ViewGroup.LayoutParams params, and set the width and height in the ViewGroup.LayoutParams.


This has been discussed before plenty. The phrase I believe you're looking for is "Dynamically adding button". It's very simple once you learn the basics of it all, and you should be able to set any of the attributes of the button dynamically (in your program) whether it be size, position, or even the onClickListener(). Look here for a basic example. Hope it helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜