天天看點

Android Activity 常用功能設定(全屏、橫豎屏等)

activity全屏設定

方式1:androidmanifest.xml

<activity android:name="myacitivty"  android:theme="@android:style/theme.notitlebar.fullscreen" />

方式2:代碼實作

requestwindowfeature(window.feature_no_title);  // 隐藏标題欄

getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen);  // 隐藏狀态欄

注意:設定全屏的倆段代碼必須在setcontentview(r.layout.main) 之前,不然會報錯。

activity橫豎屏設定

<activity android:name="myacitivty"  android:screenorientation="landscape" />  // 或者 “portrait”

setrequestedorientation(activityinfo.screen_orientation_landscape);

擷取橫屏方向

int orientation = this.getresources().getconfiguration().orientation;

orientation 的常用取值可以為 activityinfo.screen_orientation_landscape(橫屏) 或 activityinfo.screen_orientation_portrait(豎屏)

activity螢幕一直顯示

1:androidmanifest.xml添權重限

<uses-permission android:name="android.permission.wake_lock" />

2:代碼實作

getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on);