Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 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 强制横屏的方法 http://www.linuxidc.com/Linux/2011-12/49362.htmAndroid禁止横屏竖屏切换 http://www.linuxidc.com/Linux/2013-04/82437.htmAndroid全屏和强制横屏竖屏设置 http://www.linuxidc.com/Linux/2012-04/58966.htm更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11