2011-12-16 3 views
1

私は多くの古い質問に似ていますが、それは同じではありません質問があると思います。デバイスを回転させたときのレイアウトの変更を防止するにはどうすればよいですか?

私はアプリケーションの実行中にデバイスを回転させた場合のレイアウトの方向を変更しないアプリケーションを希望。 これは:

android:screenOrientation = "portrait" 

...トリックを行いません。私は固定方向でアプリケーションを持っていたくない。アプリケーションが動作しているときに変更しないようにしたいと思います。

この:

android:configChanges="orientation" 

...は、トリックを行いません。設定すると、アプリケーションは再起動しません。レイアウトは別のレイアウトに変更されませんが、現在のレイアウトは新しいデバイスの向きに従って回転して引き伸ばされます。

私が達成したいものをすべて

は、活動を開始し、その後は同じレイアウトとアプリケーションが終了するまで、同じレイアウトの向きのままの向きに応じて適切なレイアウトを選択することです。

誰でも簡単にこれを行う方法を知っていますか?

答えて

1

アプリケーションが終了するまでアプリケーションを同じ方向に制限し、呼び出されたcreateInstanceを一度使用してから、対応するリソースフォルダ内の適切なレイアウトを使用してポートレートres/layoutおよびランドスケープ用res/layout-landを使用します。 ..

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 

    int orientation = display.getOrientation(); 

    if(orientation == Configuration.ORIENTATION_PORTRAIT){ 

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    } 

    if(orientation == Configuration.ORIENTATION_LANDSCAPE){ 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    } 
+0

グレート。どうもありがとうございました! – ardabro

0

Androidのシステムが破壊し、活動を再作成することによって、あなたのために自動的に回転を処理します。 Andrdoidは、「設定変更」これを呼び出して、発生する可能性があることを、いくつかは、(http://developer.android.com/reference/android/R.attr.html#configChangesを参照)があります。

これあなたがAndroidManifest.xmlで手動で処理したい構成の変更の種類を示すことができますので、あなたが望むものではありません。おそらく、あなたは、あなたが望むような構成の変更を処理するためにActivity.onConfigurationChanged(Configuration)を上書きしたい

<Activity ... android:configChanges="orientation" /> 

/* First, get the Display from the WindowManager */ 
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 

/* Now we can retrieve all display-related infos */ 
int width = display.getWidth(); 
int height = display.getHeight(); 
int orientation = display.getOrientation(); 

とそれに応じ向き設定:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_XXXXX)

0

溶液をのonCreateに画面の向き()を見つけると、その向きを設定することです。

package com.exercise.AndroidOrientation; 

import android.app.Activity; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class AndroidOrientationActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait); 
    Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape); 

    buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){ 

@Override 
public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
}}); 

    buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){ 

@Override 
public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    }}); 
} 
} 
関連する問題