更改Android自定义SurfaceView的大小

sei*_*elj 3 android 2d surfaceview surfaceholder

我正在尝试为Android应用程序创建2D游戏引擎.我已经按照本教程,它可以很好地创建一个全屏显示,但我不希望这样.我想让我的视图占据屏幕的前2/3(或其他),并使用标准的Android小部件(按钮,文本输入等)填充底部的第三个.我不能让这个工作.我能得到的最好的是一个空白的白色屏幕.我尝试了很多排列,包括使用外部LinerLayout,然后将自定义SurfaceView嵌入到嵌套的RelativeLayout中,并将Android小部件放在嵌套的LinearLayout中,它不起作用.

例如,当我觉得它应该是50%SurfaceView时,这会产生一个白色屏幕,对于TextView,它会产生50%:

activity_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>
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

sei*_*elj 8

通过大量的谷歌搜索和半相关问题找出它.这是我如何在纵向模式(100%宽度)和2/3宽度(100%高度)横向填充高度2/3的示例.

在自定义表面视图中,覆盖onLayout方法:

@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
    if (changed) {
        (this).layout(0, 0, viewWidth, viewHeight);
    }
}
Run Code Online (Sandbox Code Playgroud)

在类构造函数中,添加以下代码:

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
}
Run Code Online (Sandbox Code Playgroud)