如何在表面视图上添加图像按钮?

时间:2015-12-15 19:45:26

标签: java android

我一直在尝试添加一个Imagebutton,但它没有用,因为我在游戏面板上使用表面视图。所以我做了这个(下面的代码),但它不起作用,所以我不知道该怎么做。请帮忙

public class settingsMenu extends Activity implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        FrameLayout game = new FrameLayout(this);
        GamePanel gameView = new GamePanel (this);

        ImageButton myButton = new ImageButton(this);


        myButton.setImageResource(R.drawable.settings);


        RelativeLayout myLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams buttonParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);

        myLayout.addView(myButton, buttonParams);

        game.addView(gameView);
        game.addView(myLayout);

        setContentView(game);
        myButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        Intent intent = new Intent(this, GamePanel.class);
        startActivity(intent);

    }
}

1 个答案:

答案 0 :(得分:0)

只需合并这两个代码:

第一个代码:

public class settingsMenu extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        FrameLayout game = new FrameLayout(this);


        ImageButton myButton = new ImageButton(this);
        ImageButton myButton2 = new ImageButton(this);

        myButton.setImageResource(R.drawable.ic_launcher);
        myButton2.setImageResource(R.drawable.abc_ab_bottom_solid_dark_holo);

        RelativeLayout myLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams buttonParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        myLayout.addView(myButton2, buttonParams);
        myLayout.addView(myButton, buttonParams);


        game.addView(myLayout);

        setContentView(game);

    }

}

现在你换一下:

myLayout.addView(myButton2, buttonParams);

myLayout.addView(myButton, buttonParams);

myLayout.addView(myButton2, buttonParams);

myLayout.addView(myButton, buttonParams);
  

您会看到上部布局覆盖较低的布局