我想为游戏插入暂停按钮和高分文本,并在游戏视图左上方的暂停按钮和右上角的高分屏文本。我想编程它,但高分和按钮不显示。下面的代码:以编程方式编辑Android SFGameView布局
gameView = new SFGameView(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView textBox = new TextView(SFEngine.context);
textBox.setText("HIGH SCORE");
textBox.setId(1);
Button pauseButton = new Button(SFEngine.context);
pauseButton.setText("PAUSE");
pauseButton.setGravity(Gravity.RIGHT);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, textBox.getId());
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(textBox);
layout.addView(pauseButton,lp);
layout.addView(gameView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
setContentView(layout,rlp);
这里的按钮监听,该按钮将不会对触摸事件
pauseButton.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e) {
try{
Thread.sleep(1000);
}
catch(InterruptedException ex){
}
return true;
}
});
我编辑了我的代码,并按照你的建议,但它仍然不会出现,屏幕只显示我的游戏。我将我的游戏视图设置为align_parent_bottom。 –
在其他两个视图之前将游戏视图添加到布局。 – nmw
它显示出来,但按钮似乎采取了所有的空间和高分文本是在它的顶部,我试图设置button.setHeight和设置重量仍然不会工作 –