Android中横屏切换的布局


Android中横屏切换的布局

切换这种效果多用在音视频播放器里面:

竖屏时这样显示:

Android中横屏切换的布局

横屏时这样显示:

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>

Android 强制横屏的方法

Android禁止横屏竖屏切换

Android全屏和强制横屏竖屏设置

相关内容