2016-07-22 222 views
1

正如標題不工作反向畫像說。當我嘗試使用反向風景時,它工作正常。這並沒有什麼意義。我做錯什麼了嗎?提前致謝。在android系統

回答

1

試試這個代碼。

public class MainActivity extends AppCompatActivity { 

    @Override protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);// turn off rotation according to accelerometer 
     Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_180);//reverse portrait 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_0);//portrait 
     Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);//turn on rotation according to accelerometer 
    } 
} 

不要忘記添加權限的AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS"/> 
+0

真棒!有用。 – philip