首页 > 代码库 > Android 横竖屏检测
Android 横竖屏检测
package com.example;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ChangeConfiguationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration_layout);
Button btn = (Button) findViewById(R.id.bn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Configuration config = getResources().getConfiguration();
// 如果是横屏则设为竖屏
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
ChangeConfiguationActivity.this
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
ChangeConfiguationActivity.this
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? "横向屏幕"
: "竖向屏幕";
Toast.makeText(ChangeConfiguationActivity.this,
"系统屏幕方向发生了变化:" + screen, Toast.LENGTH_LONG).show();
}
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ChangeConfiguationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration_layout);
Button btn = (Button) findViewById(R.id.bn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Configuration config = getResources().getConfiguration();
// 如果是横屏则设为竖屏
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
ChangeConfiguationActivity.this
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
ChangeConfiguationActivity.this
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? "横向屏幕"
: "竖向屏幕";
Toast.makeText(ChangeConfiguationActivity.this,
"系统屏幕方向发生了变化:" + screen, Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml 配置
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
android:configChanges="orientation"
<activity
android:name=".XXX"
android:configChanges="orientation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Android 横竖屏检测
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。