在android上的java文件中创建一个按钮

时间:2013-04-15 22:29:25

标签: java android layout

我正在制作游戏,目前我有这个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));

工作,请帮忙

对不起,如果这看起来很容易,我很新,仍然可以解决它。谢谢你的阅读。

2 个答案:

答案 0 :(得分:0)

你不能自己画一个按钮。您需要将自定义视图包装到XML布局中并在其中添加Button。有关示例,请参阅here。如果您真的想在代码中执行此操作,则可以以编程方式执行相同操作,但仍需要将其包装在布局中。

答案 1 :(得分: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>

然后,在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
}
相关问题