2016-04-11 75 views
0

我有一個問題。我想在景觀和potrait模式中使用該應用程序,但我需要有關何時將設備potrait旋轉到風景,應用程序分裂爲2個片段的信息。在平板電腦android中的景觀和potrait模式

我研究了許多網站,但我沒有必要的信息關於那個。任何人都可以給我一些想法我該怎麼做?

編輯

productFlavors { 
      phone { 
       applicationId "packageName.app.phone" 
       buildConfigField 'boolean', 'IsPhone', 'true' 
       versionName "" 
      } 
      tablet { 
       applicationId "packageName.app.tablet" 
       buildConfigField 'boolean', 'IsPhone', 'false' 
       versionName "" 
      } 
     } 

我拆我的apk.I有phone.apk和tablet.apk。

謝謝。

+0

您可以在layout_land文件夾中指定橫向模式。 – saeed

+0

是的,我知道這一點,但我用gradle拆分我的apk。我有一個phone.apk和一個tablet.apk.When我創建一個「layout_land」文件夾,沒有爲我工作。 –

+0

您應該看到這個文檔http://developer.android.com/intl/pt-br/guide/practices/tablets-and-handsets.html關於支持平板電腦和手機 – Gorio

回答

1

如果你真的想知道設備何時旋轉,你可以嘗試這樣的事情。

@Override 
public void onConfigurationChanged(Configuration config) { 
    super.onConfigurationChanged(config); 

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     // "landscape" 
    } else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     //"portrait" 
    } 
} 

但通常你不應該這樣做。您只需要爲portrait提供一個佈局,然後再爲lanscape提供另一個佈局,然後讓佈局加載所需的片段。

在將phone.apk和tablet.apk分開的情況下,每個apk都應該使用gradle與其各自的佈局組一起發貨。

+0

感謝@ Guillaume.I處理它與您的解決方案。 –

0

您可以檢查它的角度並做任何事情。

import android.app.Activity; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.OrientationEventListener; 
import android.widget.Toast; 

public class AndroidOrientationSensor extends Activity { 

    OrientationEventListener myOrientationEventListener; 

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

     myOrientationEventListener 
       = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { 

      @Override 
      public void onOrientationChanged(int arg0) { 
       // TODO Auto-generated method stub 
       Log.d("GORIO", "angle: " + String.valueOf(arg0)); 
      } 
     }; 

     if (myOrientationEventListener.canDetectOrientation()) { 
      Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show(); 
      myOrientationEventListener.enable(); 
     } else { 
      Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show(); 
      finish(); 
     } 
    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     myOrientationEventListener.disable(); 
    } 
}