我是Android应用程序开发的新手,试图学习自定义视图开发。下面是我创建了一个按钮的简单布局,点击该按钮将启动游戏活动。游戏活动具有自定义布局。 我的问题是,点击按钮后,它会启动新活动,但不会显示任何内容。它加载了空布局。我希望android在点击按钮时加载一个圆圈。
MainActivity.java
package com.shashi.customview2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View gamebutton=(View) findViewById(R.id.hi);
gamebutton.setOnClickListener(this);
}
public void onClick(View v)
{
Intent intent =new Intent(this,Game.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
game.java
package com.shashi.customview2;
import android.app.Activity;
import android.os.Bundle;
public class Game extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.custlayout);
}
}
自定义视图类:
package com.shashi.customview2;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
public class custview extends View
{
private int circleCol;
private Paint circlePaint;
public custview(Context context ,AttributeSet attrs)
{
super(context,attrs);
circlePaint = new Paint();
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.custview, 0, 0);
try {
//get the text and colors specified using the names in attrs.xml
circleCol = a.getInteger(R.styleable.custview_circleColor, 0);
}
finally
{
a.recycle();
}
// TODO Auto-generated method stub
}
protected void OnDraw(Canvas canvas)
{
int viewWidthHalf = this.getMeasuredWidth()/2;
int viewHeightHalf = this.getMeasuredHeight()/2;
int radius = 0;
if(viewWidthHalf>viewHeightHalf)
radius=viewHeightHalf-10;
else
radius=viewWidthHalf-10;
circlePaint.setStyle(Style.FILL);
circlePaint.setAntiAlias(true);
circlePaint.setColor(circleCol);
canvas.drawCircle(viewWidthHalf, viewHeightHalf, radius, circlePaint);
}
}
自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.shashi.customview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.shashi.customview2.custview
android:id="@+id/custView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
custom:circleColor="#ff0099"/>
</LinearLayout>