1
我想在我的android應用程序中動態設置屏幕的方向。爲此我使用android-在活動開關處意外短暫的方向變化
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
和類似的。到目前爲止,這工作得很好,但當我從一個活動轉到另一個活動時,偶爾會遇到不必要的方向轉換。當我以一種讓傳感器設置不同於編程選擇的方向的方式握住設備時會出現問題。
我能夠在下面的簡單應用程序中再現問題,該應用程序基本上由兩項活動構成,讓用戶通過按下按鈕切換到其他活動。爲了簡單起見,我將活動中的方向始終設置爲橫向,但請記住,我需要的是在運行時選擇方向,因此將方向設置移動到清單不適合我。
任何建議如何避免這種不必要的偶爾屏幕方向開關會很好。
由於
馬丁
public class A_Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_a);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Button b = (Button) findViewById(R.id.button_a);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(A_Activity.this, B_Activity.class));
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
第二活動 - B_Activity - 在按下按鈕開始A_Activity相同的方式剛剛定義
的佈局是一樣簡單
<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:orientation="vertical" >
<Button
android:id="@+id/button_a"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Click A" />
</LinearLayout>
和清單
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.switchactivitytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".A_Activity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="A Activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".B_Activity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="B Activity" >
</activity>
</application>
</manifest>
我寫你的活動 - 我需要的是在運行時選擇的方向 - 所以這對我來說不會做 – dorjeduck
還感謝對於答覆 - 我實際上寫錯了「將定位設置移到佈局不適合我」。我改變了我的想法,「將定位設置移到清單不會爲我做。」 – dorjeduck