虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > Android编程 > Android横竖屏切换属性

Android横竖屏切换属性
类别:Android编程   作者:码皇   来源:互联网   点击:

Android横竖屏切换通过在AndroidManifest xml中设置activity中的android:screenOrientation属性& 20540;来实现。
Android横竖屏切换通过在AndroidManifest.xml中设置activity中的android:screenOrientation属性值来实现。
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"

android:theme="@style/AppTheme" >
android:screenOrientation="sensor"
android:name="com.qiubai00.fragment01.MainActivity"
android:label="@string/app_name" >






该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)//横屏

相关热词搜索: 属性