我正在尝试为 Android 应用程序创建 2D 游戏引擎。我已经关注了本教程 http://www.droidnova.com/2d-tutorial-series-part-ii,772.html,这对于创建全屏显示效果很好,但我不想要这样。我想让我的视图占据屏幕的顶部 2/3(或其他位置),并用标准 Android 小部件(按钮、文本输入等)填充底部三分之一。我无法让它发挥作用。我能得到的最好的结果就是空白屏幕。我尝试了很多排列,包括使用外部 LinerLayout,然后将自定义 SurfaceView 嵌入嵌套的relativelayout中,并将 Android 小部件放入嵌套的 LinearLayout 中,但它不起作用。
例如,当我觉得 SurfaceView 应该是 50%,TextView 应该是 50% 时,这会产生白屏:
活动_main.xml:
<LinearLayout 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:baselineAligned="false" >
<RelativeLayout
android:layout_width="0dip"
android:layout_height="0dp"
android:layout_weight="1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapContainer"
>
</RelativeLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="0dp"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/white"
android:text="@string/test_str" />
</LinearLayout>
</LinearLayout>
MainActivity.java:
package com.removed.for.privacy;
import com.removed.for.privacy;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout mapContainer = (RelativeLayout) findViewById(R.id.mapContainer);
JSMapView mapView = new JSMapView(this);
mapContainer.addView(mapView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
有任何想法吗?
通过大量谷歌搜索和半相关问题弄清楚了。下面是我如何让它在纵向模式下填充 2/3 高度(100% 宽度)和在横向模式下填充 2/3 宽度(100% 高度)的示例。
在自定义surface视图中,重写onLayout方法:
@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (changed) {
(this).layout(0, 0, viewWidth, viewHeight);
}
}
在类构造函数中,添加以下代码:
public YourCustomSurfaceView(Context context) {
super(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int rotation = display.getRotation();
// If vertical, we fill 2/3 the height and all the width. If horizontal,
// fill the entire height and 2/3 the width
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
screenWidth = display.getWidth();
screenHeight = display.getHeight();
viewHeight = 2 * (screenHeight / 3);
viewWidth = screenWidth;
} else {
screenWidth = display.getWidth();
screenHeight = display.getHeight();
viewWidth = 2 * (screenWidth / 3);
viewHeight = screenHeight;
}
// Enter rest of code here
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)