0

私はゲームを作っていて、現在このJavaファイルを持っています

    package pap.crowslanding;

    import android.os.Bundle;

    public class Game extends MainActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tester1);

}

}

カスタム レイアウトの GameView を使用して、xml ファイル tester1 とマージしようとしました

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/cl_bg"
android:gravity="fill_horizontal">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="42dp"
    android:text="@string/hello_world" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="31dp"
android:orientation="horizontal" >

<Button
    android:id="@+id/play_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/custom_button"
    android:text="@string/first_button"
    android:textColor="@drawable/text_color_white" />

<Button
    android:id="@+id/sbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"      
    android:layout_weight="1"
    android:background="@drawable/custom_button"
    android:text="@string/menu_settings"
    android:textColor="@drawable/text_color_white" />
</LinearLayout>

</RelativeLayout>

<pap.crowslanding.GameView 
 android:id="@+id/GameView" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"></pap.crowslanding.GameView
>

 </RelativeLayout>

今:

setContentView(R.layout.tester1);

何らかの理由で動作しませんが、

setContentView(GameView(this));

動作します、助けてください

これが簡単に思える場合は申し訳ありませんが、私はかなり新しく、まだ頭を悩ませています。読んでくれてありがとう。

4

2 に答える 2

0

GameView と Button を含むレイアウトを作成し、それをコンテンツ ビューとして使用できます。

次のように res/layout にファイル main.xml を作成します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button Text" />

    <pap.crowslanding.GameView
        android:id="@+id/game_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

次に、onCreateActivity で、レイアウトをコンテンツ ビューとして使用するように指示します。その後、クリック リスナーなどを追加して、GameView とボタンへの参照を取得できます。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GameView gameView = (GameView) findViewById(R.id.game_view);
    Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // respond to clicks
        }
    });
}

編集: GameView クラスがそのスーパーから 3 つのコンストラクターすべてを実装していることを確認してください。

public GameView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO
}

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO
}

public GameView(Context context) {
    super(context);
    // TODO
}
于 2013-04-15T23:55:06.943 に答える