3

Android アプリ用の 2D ゲーム エンジンを作成しようとしています。全画面表示の作成には問題なく機能するこのチュートリアルに従いましたが、それは望ましくありません。画面の上部 2/3 (または何でも) をビューに表示し、下部 3 分の 1 を標準の Android ウィジェット (ボタン、テキスト入力など) で埋めたいと考えています。これを機能させることはできません。私が得ることができる最高のものは、空白の白い画面です。外側の LinerLayout の使用、ネストされた RelativeLayout 内へのカスタム SurfaceView の埋め込み、ネストされた LinearLayout への Android ウィジェットの配置など、多くの順列を試しましたが、うまくいきません。

たとえば、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>

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;
    }
}

何か案は?

4

4 に答える 4

8

多くのグーグルおよび半関連の質問を通じてそれを理解しました。これは、ポートレート モードで高さの 2/3 (幅 100%)、ランドスケープ モードで幅の 2/3 (高さ 100%) を埋める方法の例です。

カスタム サーフェス ビューで、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
}
于 2012-08-13T04:51:24.550 に答える
-1

SurfaceView のカスタムの高さと幅を変更するには、「android.view.ViewGroup.LayoutParams」を使用します。ViewGroup のさまざまなサブクラスには、LayoutParams のサブクラスがあります。たとえば、AbsoluteLayout には、X 値と Y 値を追加する LayoutParams の独自のサブクラスがあります。これでSurfaceViewのXとYを変更できます

surfaceview の高さ幅の変更コードはこちら: http://agalaxycode.blogspot.in/2014/12/change-surfaceview-height-width.html

于 2014-12-25T19:45:32.373 に答える