如何在画布上(顶部)设置一些 UI 元素?
我有一个简单的触摸游戏,其图形放置在带有画布的自定义视图上。然而,由于我的全屏面板位于 setContentView() 中,我无法添加任何 UI 项目,例如进度条或徽标。我希望整个画布层可见,一些对象(进度条、徽标)“浮动”在其上方。
主要类别:
public class GameClass extends Activity implements OnGestureListener {
Panel panel;
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
panel = new Panel(this, 1, 2);
setContentView(Panel);
...
}
面板类:
public class Panel extends View {
public Panel(Context context, int Var1, int Var2) {
super(context);
...
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
...
}
}
正如代码所示,触摸是在 GameClass 内部处理的,动画的变化是在 Panel 内部处理的。
或者也许..是否可以使用进度条和按钮启动新的透明活动,以便覆盖活动上的按钮和底层活动上的对象都可用?我可能需要一种方法来使用透明活动上的按钮关闭所有层(透明活动、面板、游戏类)。复杂的? :D
我确实有同样的问题,这就是我解决的方法
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.game);
drawView = new DrawView(this);
drawView.requestFocus();
LinearLayout upper = (LinearLayout) findViewById(R.id.LinearLayout01);
upper.addView(drawView);
ImageButton menu = (ImageButton) findViewById(R.id.ImageButton01);
menu.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
}
});
ImageButton reset = (ImageButton) findViewById(R.id.ImageButton02);
reset.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
drawView.resetGame();
}
});
}
而不是将 contentview 设置为面板,添加它 Linearlayout 添加到我的 game.xml 中,这是我的
game.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/game_background">
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="350dip" />
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/LinearLayout01">
<ImageButton
android:id="@+id/ImageButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/menu"></ImageButton>
<ImageButton
android:id="@+id/ImageButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/reset"></ImageButton>
</LinearLayout>
<LinearLayout
android:layout_below="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:id="@+id/layout_ad"
android:layout_height="wrap_content" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)