编辑:从原网站导入教程
如何在 AndEndine 中使用 UI Android SDK
注意:如果您在这些布局中更改宽度和高度,可能会出现填充错误,因此请小心(此解决方案适用于全屏使用)
XML 布局
在您的项目目录res/layout中创建一个名为themainactivity.xml的空文件,并在其中放入以下内容。
注意:将属性tools:context 设置为您的应用程序活动名称,以点开头(此处:.MyMainActivity)
XML 布局文件:res/layout/themainactivity.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- code placed here will be above the AndEngine render -->
</RelativeLayout>
Java 类
你只需要在你的类中指定 ID。
MyMainActivity.java
package com.example;
import org.andengine.ui.activity.SimpleLayoutGameActivity;
public class MyMainActivity extends SimpleLayoutGameActivity
{
@Override
protected int getLayoutID()
{
return R.layout.themainactivity;
}
@Override
protected int getRenderSurfaceViewID()
{
return R.id.gameSurfaceView;
}
}
在 AndEngine 中创建自定义 Android SDK 布局
XML 布局
警告:根节点必须是合并节点!在里面你可以做你想做的事。
XML 布局文件:res/layout/my_view.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:text="Dat button" />
</LinearLayout>
</merge>
控制器类
为了能够使用您的界面,您必须使用 inflater 服务将其链接到 XML 视图。
注意:UI Java 代码是在您切换到 WYSIWIG 编辑器时编译的,因此如果您不添加下面的链接代码,您将不会在使用它的活动中看到布局的内容。
自定义布局:MyView.java
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
public class MyView extends LinearLayout
{
public MyView(Context context, AttributeSet attrs)
{
super(context, attrs);
// Link to the XML view
LayoutInflater.from(context).inflate(R.layout.my_view, this, true);
// Link to the XML view (alternative using service, you can delete if you don't need it)
//LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflater.inflate(R.layout.my_view, this);
}
}
在其他活动中重复使用
只需在活动布局中添加此代码即可。
<com.example.MyView
android:id="@+id/myView1"
android:layout_width="100dp"
android:layout_height="wrap_content" />