80,470
社区成员




protected void onCreate(Bundle savedInstanceState) {
Log.v("GameWindow", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.gameview);
// LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
// View layout = inflater.inflate(R.layout.gameview,null);
// gameView = (GameView) view.findViewById(R.id.game_sudo_view);
gameView = (GameView) findViewById(R.id.game_sudo_view);
if(gameView == null){
Log.v("gameView", "onCreate->null");
}
levelTextView = (TextView)findViewById(R.id.leveltextview);
levelTextView.setText("Topic."+1));
if(levelTextView == null){
Log.v("TextView", "onCreate->null");
}
}
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/back">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:contentDescription="@string/app_name"
android:src="@drawable/grass" />
<com.ljx.gameview.GameView
android:id="@+id/game_sudo_view"
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_centerHorizontal="true"
android:layout_above="@id/game_sudo_view"
android:id="@+id/leveltextview"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.myview, null);
ImageView view1=(ImageView)view.findViewById(R.id.imageview);
这样之后就可以获取到正确的控件了LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.gameview,null);
gameView = (GameView) view.findViewById(R.id.game_sudo_view);
也试过clean项目了,但不知怎么解决public class GameView extends View{
……
public GameView(Context context,AttributeSet a){
……
}
//自定义view的宽高
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = game.getScreenWidth();
int height = game.getScreenHeight()-game.getScreenHeight()/4;
setMeasuredDimension(width, height);
//自定义view的宽高时,不实用下面函数
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@SuppressLint("DrawAllocation") @Override
protected void onDraw(Canvas canvas) {
……
super.onDraw(canvas);
}
……
}
public static final class id {
……
public static final int gallery01=0x7f080018;
public static final int game_sudo_view=0x7f080011;
public static final int gamegridview=0x7f080010;
public static final int grid_image=0x7f080013;
public static final int grid_text=0x7f080014;
public static final int leveltextview=0x7f080012;
……
}
public static final class id {
……
public static final int gallery01=0x7f080018;
public static final int game_sudo_view=0x7f080011;
public static final int gamegridview=0x7f080010;
public static final int grid_image=0x7f080013;
public static final int grid_text=0x7f080014;
public static final int leveltextview=0x7f080012;
……
}