Android横竖屏切换属性
2015-06-21 17:21
309 查看
Android横竖屏切换通过在AndroidManifest.xml中设置activity中的android:screenOrientation属性值来实现。
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:screenOrientation="sensor"
android:name="com.qiubai00.fragment01.MainActivity"
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>
该android:screenOrientation属性,他有以下几个参数:
"unspecified":默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.
"landscape":横屏显示(宽比高要长)
"portrait":竖屏显示(高比宽要长)
"user":用户当前首选的方向
"behind":和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)
"sensor":有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。
"nosensor":忽略物理感应器,这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。
比如下列设置
android:screenOrientation="portrait"
则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。
android:screenOrientation="landscape",为横屏显示。
上述修改也可以在Java代码中通过类似如下代码来设置
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
//在MainActivity中获取屏幕是横屏还是竖屏:
DisplayMetrics dm=new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
Fragment1 fragment1=new Fragment1();
Fragment2 fragment2=new Fragment2();
FragmentManager fm=this.getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
if(width>height)//横屏
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:screenOrientation="sensor"
android:name="com.qiubai00.fragment01.MainActivity"
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>
该android:screenOrientation属性,他有以下几个参数:
"unspecified":默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向.
"landscape":横屏显示(宽比高要长)
"portrait":竖屏显示(高比宽要长)
"user":用户当前首选的方向
"behind":和该Activity下面的那个Activity的方向一致(在Activity堆栈中的)
"sensor":有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。
"nosensor":忽略物理感应器,这样就不会随着用户旋转设备而更改了("unspecified"设置除外)。
比如下列设置
android:screenOrientation="portrait"
则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。
android:screenOrientation="landscape",为横屏显示。
上述修改也可以在Java代码中通过类似如下代码来设置
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
//在MainActivity中获取屏幕是横屏还是竖屏:
DisplayMetrics dm=new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
Fragment1 fragment1=new Fragment1();
Fragment2 fragment2=new Fragment2();
FragmentManager fm=this.getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
if(width>height)//横屏
相关文章推荐
- androidStudio --快捷键
- 基于Android的Web Kit性能对比测试
- 基于Android的ELF PLT/GOT符号和重定向过程ELF Hook实现(by 低端农业代码 2014.10.27)
- Android Material Design之Toolbar与Palette实践
- android ListView详解
- 一步步走进Android MaterialDesign 之 其余小控件
- Android解析xml文件获取数据练习
- 对android中ActionBar中setDisplayHomeAsUpEnabled和setHomeButtonEnabled和setDisplayShowHomeEnabled方法的理解
- android在Canvas使用drawBitmap画一幅画
- Android Studio开发第三篇版本管理Git
- Android计时器
- AndroidPN的学习研究(一)环境建立
- 一步步走进Android MaterialDesign 之 ToolBar动画效果(2)
- Android事件传递机制【Touch事件】
- Android事件传递机制【按键事件】
- 详解——android系统软键盘的手动显示和隐藏
- Android开发通过adapter显示listview
- Android开发中执行方法中含有监听时的处理
- 解决android中出现的无法加载library的问题
- 解决android中出现的无法加载library的问题