Android横竖屏切换属性

来源:互联网 发布:淘宝天天疯狂购 编辑:程序博客网 时间:2024/05/02 02:36
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)//横屏

0 0