Android中橫屏切換的布局
切換這種效果多用在音視頻播放器裡面:
豎屏時這樣顯示:
橫屏時這樣顯示:
activity代碼:
package com.tmacsky;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class RotateSampleActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lan);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
//啟動時默認是豎屏
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.portrait);
}
//切換就是橫屏
else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.lan);
}
}
}
layout文件直接在編輯器裡拖4個button就可以了,水平布局lan和垂直布局portrait 2個layout文件
主要的是此時要在manifest.xml文件中添加權限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tmacsky"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
//給一個旋轉後的權限
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
<activity android:name=".RotateSampleActivity"
android:configChanges="orientation|locale"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>