我正在尝试在Android中创建一个简单的球和棒游戏,并为android提供以下xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BallActivity">
<learn.com.application.BallView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ball"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
android:onClick="moveleft"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:onClick="moveright"/>
</LinearLayout>
</LinearLayout>
包含按钮的LinearLayout不可见,因为BallView是match_parent。但是,如果我将BallView的高度声明为wrap_content它不起作用。如何在底部显示左右按钮。还有什么方法可以用来左右移动棍子。
BallView代码
public class BallView extends android.support.v7.widget.AppCompatImageView {
private Context mContext;
int x = -1;
int y = -1;
boolean first=true;
private int xVelocity = 10;
private int yVelocity = 5;
private Handler h;
public static int FRAME_RATE = 50;
public static int stickx=0,sticky=0;
public static int width=0;
public static int height=0;
boolean gameover=false;
public BallView(Context context,AttributeSet s) {
super(context,s);
mContext=context;
h=new Handler();
}
private Runnable r=new Runnable() {
@Override
public void run() {
invalidate();
}
};
@Override
protected void onDraw(Canvas canvas) {
if(first) {
stickx = getWidth() / 2 - 40;
sticky = getHeight() - 60;
width = getWidth();
height = getHeight();
first=false;
}
Bitmap bmp= BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ball1);
if(x<0 && y<0)
{
x=getWidth()/2;
y=getHeight()/2;
}
else {
x += xVelocity;
y += yVelocity;
if (x + bmp.getWidth() > getWidth() || (x < 0))
xVelocity = xVelocity * -1;
if (y < 0)
yVelocity = yVelocity * -1;
if (y + bmp.getHeight() >= sticky) {
if (x + bmp.getWidth() > stickx && x + bmp.getWidth() < (stickx + 180)) {
yVelocity = yVelocity * -1;
Log.v("ball","collision");
}
else {
Log.v("ball", "game over");
gameover=true;
}
}
}
RectF rr=new RectF();
int right=stickx+180;
int bottom=sticky+30;
rr.set(stickx,sticky,right,bottom);
Paint p=new Paint();
canvas.drawRoundRect(rr,5,5,p);
canvas.drawBitmap(bmp,x,y,null);
if(!gameover)
h.postDelayed(r,FRAME_RATE);
}
}
答案 0 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BallActivity">
<learn.com.application.BallView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tablerow"
android:id="@+id/ball"/>
<TableRow
android:id="@+id/tablerow"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Left"
android:onClick="moveleft"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Right"
android:onClick="moveright"/>
</TableRow>
</RelativeLayout>
我不想使用LinearLayout,而是更喜欢使用RelativeLayout和TableRow,如图所示