Sep*_*phy 302 android screen rotation
我有一个我想阻止旋转的活动,因为我正在启动AsyncTask,屏幕旋转使它重新启动.
有没有办法告诉这个活动"即使用户像疯了一样摇动他的手机也不要旋转屏幕"?
lbe*_*gni 445
加
android:screenOrientation="portrait"
Run Code Online (Sandbox Code Playgroud)
要么
android:screenOrientation="landscape"
Run Code Online (Sandbox Code Playgroud)
<activity>
清单中的元素,你已经完成了.
Emr*_*ici 124
你可以按照下面的逻辑,以防止自动旋转屏幕,而你AsyncTask
正在运行:
getRequestedOrientation()
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)
.AsyncTask
.AsyncTask
恢复以前的方向状态setRequestedOrientation(oldOrientation)
.请注意,有几种方法可以访问Activity
(在UI线程上运行)的属性AsyncTask
.您可以将您AsyncTask
的内容类实现为内部类,也可以使用Handler
戳您的Activiy
类的消息.
Sar*_*ara 26
在您的清单文件中,对于要锁定屏幕旋转的每个活动添加:如果要以水平模式锁定它:
<activity
...
...
android:screenOrientation="landscape">
Run Code Online (Sandbox Code Playgroud)
或者如果要在垂直模式下锁定它:
<activity
...
...
android:screenOrientation="portrait">
Run Code Online (Sandbox Code Playgroud)
Pau*_*der 23
我发现这样做最简单的方法就是放
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)
在onCreate之后,就在之后
setContentView(R.layout.activity_main);
Run Code Online (Sandbox Code Playgroud)
所以...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以这样做,而不是进入AndroidManifest:
screenOrientation = getResources().getConfiguration().orientation;
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
... AsyncTask
screenOrientation = getResources().getConfiguration().orientation;
@Override
protected void onPostExecute(String things) {
context.setRequestedOrientation(PlayListFragment.screenOrientation);
or
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
Run Code Online (Sandbox Code Playgroud)
这里唯一的缺点是它需要API级别18或更高.所以基本上这是矛的尖端.
Activity.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port
}
} catch (Exception ex) {
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="QRCodeActivity" android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
208271 次 |
最近记录: |