简体   繁体   中英

How to display a simple ImageButton on a customly created view?

I've been browsing the entire internet, but I could not find any answer to this question. I want to create a background, some image buttons, along with a bunch of moving graphics (circles). Since I do not know how to overwrite a xml layout with moving graphics, I chose to customly create my view, draw the background, the imageButtons and the circles (2D graphics).

public class GameView extends View implements UtilConstants  

since I extended the View class, I had to call the super class constructor in my constructor

GameView(Context context) {
    super(context);
    this.context = context;
    this.paint = new Paint();
}

and to implement the onDraw method, which acts like the java paintComponent

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas); // necessary for some reason

    setGridBackground(); // sets the background

    paint.setColor(Color.parseColor("#FF0000"));
    canvas.drawCircle(this.x += 10, 300, 20, paint); //moves my graphics (ball) within the screen

    drawScoreInLeftUpperCorner(canvas, 20); // paints a string text on the screen

    setAbilitiesImages(); // places some imageButtons

    try {
        Thread.sleep(THREAD_REFRESH_RATE_MS);
    } catch (InterruptedException ex) {
        System.out.println(ex);
    }

    invalidate();
}

Now, to get to my problem: I do not know how to set those ImageButtons !!! I am trying

 private void setAbilititesImages() {
     ImageButton imageButton = new ImageButton(this.context); // is this ok to put the argument this.context??

    imageButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    imageButton.setImageResource(R.drawable.monster); // set my resource

    RelativeLayout relativeLayout = findViewById(R.id.in_game_layout); // what is the purpose of this???

    if (relativeLayout != null) { // it never enters this if, relativeLayout is always null
        relativeLayout.addView(imageButton);
    }
 }

Aaaaand the image button never shows up...why is it necessary to use Relative/Linear layouts? That R.id.in_game_layout I created is just an empty RelativeLayout xml. Can't I just use something like

imageButton.setImageResource(R.drawable.monster); // set my resource
imageButton.setBounds(.....);
(View)this.add(imageButton); ???

ImageButton requires a parent view to be hosted in it. The parent View can be a RelativeLayout, a linearlayout or a ConstraintLayout.

In your case, the instance relativeLayout is always null because you not specify the view parent in which you do findViewById.

You should make something like :

RelativeLayout relativeLayout = view.findViewById(R.id.in_game_layout);

You need to add layout param to your parent Relative layout also to display your Image button appropriately.

Example

 RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    ImageButton imageButton = new ImageButton(this);
    imageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_background,null));
    relativeLayout.addView(imageButton,params);

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