动态更改 Android 的屏幕方向
2010-03-05 18:25:00 来源:WEB开发网核心提示:private Button bt;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//bt = (Button)find
private Button bt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
bt = (Button)findViewById(R.id.bt_push);
bt.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
}
Step 4:在AndroidManifest.xml文件里设置默认方向,不然程序不能正常工作哦.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.chgandroidorientation" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ChgAndroidOrientation" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <act <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
更多精彩
赞助商链接