开发者

How to create button in my own View

开发者 https://www.devze.com 2023-03-22 10:02 出处:网络
I\'m creating simple game to learn game development\'s basics on Android. there is class GameView extends View

I'm creating simple game to learn game development's basics on Android.

there is

class GameView extends View

I'm extend View (should I extend View? or ViewGroup? or Any Other? - There are basicly bitmaps in the game and may be some custom wigets)

There are sprites in this view and I want to create custom "STRIKE" button using ImageButton (should I use ImageButton wiget?)

How can I add wiget without using XML layout(!)

//for example simple Button
class GameView ext开发者_C百科ends View
{
    private Button btn;


    public GameMainView(Context context)
    {
        super(context);
        btn = new Button(context);      
    }
}

How to display this Button in my game?


You should extend ViewGroup: when you override onDraw you will get a Canvas. You'll have to manually set the position of the Button view but if you do that then call its draw method with the Canvas it should display fine.

EDIT: http://developer.android.com/guide/topics/ui/custom-components.html has the info you need. Look at the Compound Controls section.


 Button btnabout = (Button) findViewById(R.id.btnAbout);
    btnabout.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View V) 
        {
            //do something
        }//end onClick          
    });//end button
0

精彩评论

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