Android如何横屏, 全屏, 背景灯常量

横屏方法

1.在AndroidManifest.xml中的Activity加入android:screenOrientation="landscape"

配置文件中对Activity节点添加android:screenOrientation属性(landscape是横向,portrait是纵向)

<activity android:name=".TestActivity" android:screenOrientation="landscape" 
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

2.在Java的Activity中写入以下方法:

要设置成竖屏设置成SCREEN_ORIENTATION_PORTRAIT

@Override
	protected void onResume() {
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		super.onResume();
	}

全屏方法

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
         //设置全屏显示
         requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
}

保持背景灯常亮

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.main);
}

相关推荐