I'm making a game and currently I have this java file
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);
}
}
Using my custom layout GameView, I have tried to merge it with my xml file 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>
Now:
setContentView(R.layout.tester1);
Will not work for some reason but
setContentView(GameView(this));
Works, any help please
Sorry if this seems easy, I'm quite new and still getting my head around it. Thank you for reading.
You cannot draw a Button yourself. You need to wrap your custom View into an XML layout and add the Button there. See here for an example. You can do the same programmatically, if you really want to do it in the code, but you still gonna need to wrap it in a layout.
You could create a layout that contains your GameView and a Button, and use it as your content view.
Create a file main.xml in res/layout like this:
<?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>
Then, in onCreate
in your Activity, tell it to use the layout as the content view. You can then grab a reference to your GameView and the Button, adding click listeners or whatnot.
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
}
});
}
Edit: Make sure your GameView class implements all three constructors from its super:
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.