簡體   English   中英

在Android上的Java文件中創建按鈕

[英]Creating a button in java file on android

我正在做一個游戲,目前我有這個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));

可行,請幫忙

抱歉,如果這看起來很容易,我是個新手,但我仍會繼續努力。 感謝您的閱讀。

您不能自己繪制一個按鈕。 您需要將自定義視圖包裝到XML布局中,然后在其中添加Button。 請參閱此處的示例。 如果您確實想在代碼中執行此操作,則可以通過編程方式執行相同操作,但仍需要將其包裝在布局中。

您可以創建一個包含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>

然后,在Activity中的onCreate中,告訴它使用布局作為內容視圖。 然后,您可以獲取對GameView和Button的引用,添加點擊偵聽器或其他。

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類從其超級實現所有三個構造函數:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM