Java 如何在自定义创建的视图上显示简单的ImageButton?

Java 如何在自定义创建的视图上显示简单的ImageButton?,java,android,imagebutton,Java,Android,Imagebutton,我浏览了整个互联网,但我找不到这个问题的任何答案。我想创建一个背景,一些图像按钮,以及一组移动的图形(圆圈)。因为我不知道如何用移动的图形覆盖xml布局,所以我选择自定义创建视图、绘制背景、图像按钮和圆圈(2D图形) 因为我扩展了视图类,所以我必须在构造函数中调用超类构造函数 GameView(Context context) { super(context); this.context = context; this.paint = new Paint(); } 并实

我浏览了整个互联网,但我找不到这个问题的任何答案。我想创建一个背景,一些图像按钮,以及一组移动的图形(圆圈)。因为我不知道如何用移动的图形覆盖xml布局,所以我选择自定义创建视图、绘制背景、图像按钮和圆圈(2D图形)

因为我扩展了视图类,所以我必须在构造函数中调用超类构造函数

GameView(Context context) {
    super(context);
    this.context = context;
    this.paint = new Paint();
}
并实现onDraw方法,其作用类似于javapaintComponent

现在,来解决我的问题: 我不知道如何设置这些图像按钮!!!我正在努力

 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);
    }
 }
aa而且图像按钮从未显示…为什么需要使用相对/线性布局?我创建的R.id.in_game_布局只是一个空的RelativeLayout xml。我不能用像这样的东西吗

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

ImageButton要求在其中承载父视图。父视图可以是RelativeLayout、linearlayout或ConstraintLayout

在您的情况下,实例relativeLayout始终为null,因为您没有指定在其中查找DViewById的视图父级

你应该做如下的事情:


RelativeLayout RelativeLayout=view.findViewById(游戏布局中的R.id)

您还需要将布局参数添加到父相对布局中,以适当显示图像按钮

范例

 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);

但是,既然类扩展了视图,那么这样说还不够吗;或者简单地查找视图id(游戏布局中的R.id);(和这个一样。)。?因为我假设视图父对象是这样的——它抛出java.lang.NullPointerException:尝试在relativeLayout.addView(imageButton,params)的空对象引用上调用虚拟方法“void android.view.ViewGroup.addView(android.view.view,android.view.ViewGroup$LayoutParams)”;错误表示相对布局为空,如果仍然没有解决,请检查并添加有关错误的更多详细信息。事实上,relativeLayout为空。在您的代码和我的代码中,我都使用了RelativeLayout RelativeLayout=findViewById(R.id.In_game_layout);->这是唯一一条覆盖了relativeLayout的指令,这是我问的问题的一部分:为什么它总是空的?我应该在游戏布局中向您展示xml布局吗?都德,我附加的代码测试了代码,只有它在工作。同样显示xml,问题可能是其他问题。
imageButton.setImageResource(R.drawable.monster); // set my resource
imageButton.setBounds(.....);
(View)this.add(imageButton); ???
 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);