2015-11-03 37 views
0

我有一個服務,在啓動一個視圖出現在所有視圖之上。Android:如何設置視圖方向

如果我將手機旋轉到風景,則視圖方向將變爲風景,但我只想製作視圖人像。我怎樣才能做到這一點?

+0

將清單文件中的'android:screenOrientation =「portrait」'添加到您擁有視圖的活動中。 ' –

+0

這不是一個活動..我的觀點是打開時,音量減小按鈕按.. –

+1

但你的看法必須在裏面的東西?告訴我們你的代碼。 –

回答

0

嘗試在視圖打開時以編程方式設置方向。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

+0

這不是一個活動.. –

+0

是的,但你已經在任何活動的情況下展示了這個觀點。 –

+0

是的..爲了這個活動,我已經設置了清單以及在Oncreate()的屏幕方向 –

0

在您的清單文件一定要設置正確的方向,就像這樣:

<activity android:name=".SomeActivity" 
android:label="@string/app_name" 
android:screenOrientation="portrait" /> 

這樣肖像模式是被迫的,當你旋轉你的設備的應用程序不會改變爲橫向。

+0

這不是一個活動.. –

+0

那麼你想要做什麼?讓整個應用程序改變方向,但解決這個單一的視圖肖像? – LilaQ

2
@Override 
public void onCreate(Bundle savedInstanceState) { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } 

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
+0

這不是一個活動.. –

+0

將它添加到您的活動,將工作@Sangha_development – dnkilic

+0

不工作我已經做到了 –

1

要聲明你的活動處理的配置變化,在你的清單文件編輯適當<activity>元素包括android:configChanges屬性與代表要處理的配置值。

例如,下面的清單代碼聲明,說明如何處理屏幕方向變化和鍵盤可用性的變化的活動:

<activity android:name=".MyActivity" 
      android:configChanges="orientation|keyboardHidden" 
      android:label="@string/app_name"> 

現在,當這些配置的一個改變,MyActivity不會重新啓動。相反,MyActivity接收到對onConfigurationChanged()的調用。此方法傳遞一個Configuration對象,該對象指定新的設備配置。通過閱讀配置中的字段,您可以確定新的配置並通過更新界面中使用的資源進行適當的更改。

例如,以下onConfigurationChanged()實現檢查當前設備取向:

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

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
    } 
} 

或簡稱

的screenOrientation是活性元素的屬性。 android活動的方向可以是縱向,橫向,傳感器,未指定等。您需要在AndroidManifest.xml文件中定義它。例如:

<activity 
      android:name="com.example.screenorientation.MainActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="landscape" 
      > 

但按我的經驗,你需要設置是面向Android 3.0的。2以上,你既需要

android:configChanges="orientation|screenSize" 

參考:http://developer.android.com/guide/topics/resources/runtime-changes.html

編輯:

Android的屏幕方向生命週期

我們可能對Android的屏幕方向有些混亂改變Android活動的生命週期。有時您可能會觀察到您的活動重新啓動,而您旋轉設備。有時沒有任何反應以下是android屏幕方向更改生命週期的簡單規則。

。如果您已在您的java代碼(在您的Android活動類中)中使用@Override 函數來處理Android Screen Orientation Change。那麼您的活動將永遠不會重新啓動任何屏幕方向更改。

。如果您沒有如上所述的@Override onConfigurationChanged()功能,那麼您的運行活動每次都會在您的設備發生任何屏幕方向更改時重新啓動。這意味着您的活動首先會通過調用onDestroy() API銷燬,然後onCreate()方法將再次調用您的運行活動以重新啓動它。

。要正確處理您的活動的重新啓動狀態,您需要通過正常的活動生命週期恢復其以前的狀態,在該生命週期中,Android操作系統會在其破壞活動之前調用onSaveInstanceState()方法。這樣你就可以保存你的數據(變量值)和你的應用程序狀態。您可以在onCreate()onRestoreInstanceState()方法調用您的活動期間恢復以前的狀態。

希望它能幫助你。

+0

這不是一個活動..請仔細閱讀問題.. –

+0

@ Sangha_development:那麼你有哪些觀點? – KishuDroid

0

請在下面找到一個更好的主意來實現您的任務的例子。

public class MainScreenOrientation extends Activity { 

    private static final String TAG = "MainScreenOrientation"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mContext = this; 

     setContentView(R.layout.main_activity); 

     startService(new Intent(this, MyOwnService.class)); 
    } 
} 

在我MyOwnService服務類

public class MyOwnService extends Service { 
    private static final String TAG = "MyOwnService"; 
    private static Context mContext; 
    private static final String BCAST_CONFIGCHANGED = "android.intent.action.CONFIGURATION_CHANGED"; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     mContext = this; 

     IntentFilter intentFilter = new IntentFilter(); 
     intentFilter.addAction(BCAST_CONFIGCHANGED); 
     this.registerReceiver(mBroadcastReceiver, intentFilter); 
    } 

    @Override 
    public void onDestroy() {   
     Log.d(TAG, "onDestroy()"); 
     //Unregister receiver to avoid memory leaks 
     mContext.unregisterReceiver(mBroadcastReceiver); 
    } 

    @Override 
    public void onStart(Intent intent, int startid) { 

    } 

    public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent myIntent) { 

      if (myIntent.getAction().equals(BCAST_CONFIGCHANGED)) { 

       Log.d(TAG, "received->" + BCAST_CONFIGCHANGED); 


       if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){ 
        // it's Landscape 
        Log.d(TAG, "LANDSCAPE"); 
       } 
       else { 
        Log.d(TAG, "PORTRAIT"); 
       } 
      } 
     } 
    }; 
} 

,最後清單文件

<service android:enabled="true" android:name="com.android.example.services.MyOwnService"> 
      <intent-filter> 
       <action android:name="android.intent.action.CONFIGURATION_CHANGED"/> 
      </intent-filter> 
</service> 

希望它可以幫助你。

0

如果我理解正確,您希望您的視圖在人像模式下得到修復,但您的應用的其餘部分可在您旋轉設備時自由旋轉。

在這種情況下,您需要捕捉方向更改,然後將您的視圖逆時針旋轉到當前的旋轉。

事情是這樣的:

@Override 
public void onConfigurationChanged(Configuration newConfig) 
{ 
    super.onConfigurationChanged(newConfig); 
    View yourView = ....; 

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    { 
     yourView.setRotation(-90); 
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 
    { 
     yourView.setRotation(90); 
    } 
} 

你也有你的清單文件編輯相應的元素。

 <activity android:name=".MyActivity" 
     android:configChanges="orientation|keyboardHidden" 
     android:label="@string/app_name"> 

我不確定度數值,現在不能檢查這個,你可能不得不測試這個。

儘管如此,這種方式可以讓您的視圖始終處於縱向模式。

相關問題