當前位置:首頁 » 安卓系統 » android代碼豎屏

android代碼豎屏

發布時間: 2022-09-28 20:04:59

⑴ android怎麼設置橫豎屏切換

方法一:

1、首先在android手機打開「設置」這個選項的,點擊「顯示」這個功能;

⑵ android系統強制橫豎屏實現

橫屏:
按照下面代碼示例修改Activity的onResume方法
@Override
protected void onResume() {
/**
* 設置為橫屏
*/
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
super.onResume();
}

或者在配置文件中對Activity節點添加android:screenOrientation屬性(landscape是橫向,portrait是縱向)
android:launchMode="singleTask" android:screenOrientation="portrait">

豎屏:
要設置成豎屏將上面代碼中,屬性設置成 SCREEN_ORIENTATION_PORTRAIT即可。

⑶ android如何禁止橫豎屏切換

禁用橫豎屏切換,有兩種方式,第一種是在配置文件中配置,第二種是在java代碼中設置。

第一種是在AndroidManifest.xml中,為activity節點配置android:screenOrientation屬性,指定該activity為豎屏或橫屏,我們將上面的這個activity的配置為豎屏,如下:

<activity
android:name="chengyujia.androidtest.OrientationActivity"
android:screenOrientation="portrait" />
再運行測試,此時無論手機屏幕方向如何,該activity始終是豎屏的。如果android:screenOrientation="landscape" 則始終是橫屏。

下面來看第二種,

只要在onCreate方法中加一句

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
即可始終保持豎屏,如果要橫屏,代碼是

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

⑷ 怎麼用Android代碼鎖定整個app豎屏

方法:用壓縮軟體打開apk文件,解壓出根目錄中的classes.dex文件 使用cmd ,dex2jar.bat classes.dex命令將classes.dex轉換為jar 再用jd-gui打開該jar就可以查看源碼了,如果apk安全性好的話,有些代碼是看不到的。

⑸ 怎麼用Android代碼鎖定整個app豎屏

清單注冊文件:android:screenOrientation=""

<activity
android:name="..."
android:screenOrientation="...">

⑹ android如何在代碼中判斷橫豎屏

Android中判斷橫豎屏是通過Configuration 這個類來判斷的。

  1. Configuration.ORIENTATION_LANDSCAPE 表示橫屏。

  2. Configuration.ORIENTATION_PORTRAIT表示豎屏。


以下是完整代碼:

public boolean isScreenChange() {

Configuration mConfiguration = context.getResources().getConfiguration(); //獲取設置的配置信息
int ori = mConfiguration.orientation ; //獲取屏幕方向
if(ori == Configuration.ORIENTATION_LANDSCAPE){
//橫屏
return true;
}else if(ori == Configuration.ORIENTATION_PORTRAIT){
//豎屏
return false;
}
return false;
}


避免在轉屏時重啟Activity

android中每次屏幕方向切換時都會重啟Activity,所以應該在Activity銷毀前保存當前活動的狀態,在Activity再次 Create的時候載入配置,那樣,進行中的游戲就不會自動重啟了!
要避免在轉屏時重啟Activity,可以通過在AndroidManifest.xml文件中重新定義方向(給每個Activity加上android:configChanges=」keyboardHidden|orientation」屬性)。
在需要控制屏幕顯示方向的Activity中重寫onConfigurationChanged(Configuration newConfig)方法,這樣在轉屏時就不會重啟Activity了。

⑺ 如何設置android界面橫屏顯示

全屏

在Activity的onCreate方法中的setContentView(myview)調用之前添加下面代碼

requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//設置全屏

橫屏

按照下面代碼示例修改Activity的onResume方法

@Override
protected void onResume() {
/**
* 設置為橫屏
*/
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
super.onResume();
}

或者在配置文件中對Activity節點添加android:screenOrientation屬性(landscape是橫向,portrait是縱向)

android:launchMode="singleTask" android:screenOrientation="portrait">

要設置成豎屏設置成 SCREEN_ORIENTATION_PORTRAIT

// ----------------

常亮

view.setKeepScreenOn(true)

不加任何旋轉屏幕的處理代碼的時候,旋轉屏幕將會導致系統把當前activity關閉,重新打開。
如果只是簡單的界面調整,我們可以阻止此問題的發生,屏幕旋轉而自己調整屏幕的元素重構。
首先我們需要修改AndroidManifest.xml文件:
<activity android:name=".Magazine">
</activity>
//修改為:
<activity android:name=".Magazine"
android:configChanges="orientation|keyboard">
</activity>
這樣是讓程序能夠響應旋轉屏幕的事件。
然後重寫onConfigurationChanged方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.v(" == onConfigurationChanged");
processLayout();
}

//----------------------------

在我們用Android開發過程中,會碰到Activity在切換到後台或布局從橫屏LANDSCAPE切換到PORTRAIT,會重新切換Activity會觸發一次onCreate方法。

在Android開發中這種情況視可以避免的,我們可以在androidmanifest.xml中的activit元素加入這個屬性 android:configChanges="orientation|keyboardHidden" 就能有效避免oncreat方法的重復載入,

androidmanifest.xml內容如下:紅色字體為添加部分

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DemoGPS"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />

</application>
<uses-sdk android:minSdkVersion="7" />

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

</manifest>

同時在Activity的Java文件中重載onConfigurationChanged(Configuration newConfig)這個方法,這樣就不會在布局切換或窗口切換時重載等方法。代碼如下:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//land
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//port
}
}

//------------------------------------------------------

關於Android中Activity的橫豎屏切換問題可以通過AndroidManifest.xml文件中的Activity來配置:

android:screenOrientation=["unspecified" | "user" | "behind" |
"landscape" | "portrait" |
"sensor" | "nonsensor"]

screenOrientation 用來指定Activity的在設備上顯示的方向,每個值代表如下含義:

"unspecified" 默認值 由系統來判斷顯示方向.判定的策略是和設備相關的,所以不同的設備會有不同的顯示方向.

"landscape" 橫屏顯示(寬比高要長)

"portrait" 豎屏顯示(高比寬要長)

"user" 用戶當前首選的方向

"behind" 和該Activity下面的那個Activity的方向一致(在Activity堆棧中的)

"sensor" 有物理的感應器來決定。如果用戶旋轉設備這屏幕會橫豎屏切換。

"nosensor" 忽略物理感應器,這樣就不會隨著用戶旋轉設備而更改了 ( "unspecified"設置除外 )。

更多安卓例子請去360手機助手下載安卓學習手冊,裡面有橫豎排例子,源碼,例子隨便看。

⑻ android開發橫豎屏問題

Android橫屏豎屏設置

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//設置成全屏模式

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE););//強制為橫屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//豎屏
我做的東西裡面還用到了去掉標題欄。
我也貼出來
requestWindowFeature(Window.FEATURE_NO_TITLE);

垂直居中:

android:layout_centerVertical="true"

水平居中:

android:layout_centerHorizontal="true"

1.hideStatusbarAndTitlebar()隱藏statusbar和titlebar.

private void hideStatusbarAndTitlebar() {
final Window win = getWindow();
// No Statusbar
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// No Titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
2.設置屏幕顯示模式ScreenOrientation.

在activity里設置android:screenOrientation的值。
android:screenOrientation的屬性有以下值:
unspecified(默 認值,由系統判斷狀態自動切換),The default value. The system chooses the orientation. The policy it uses, and therefore the choices made in specific contexts, may differ from device to device.
landscape,橫屏
portrait,豎屏
user(用戶當前設置的orientation值),The user's current preferred orientation.
behind(下一個要顯示的Activity的orientation值),The same orientation as the activity that's immediately beneath it in the activity stack.
sensor(傳 感器的方向),The orientation determined by a physical orientation sensor. The orientation of the display depends on how the user is holding the device; it changes when the user rotates the device.
nosensor(不 使用感測器,這個效果差不多等於unspecified).An orientation determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device. Except for this distinction, the system chooses the orientation using the same policy as for the "unspecified" setting.

3.水平/垂直居中的方法.

設置parent的android:gravity為"center"。

4.獲得當前屏幕寬高的方法.

Display display = getWindowManager().getDefaultDisplay();
Config.screenWidth = display.getWidth();
Config.screenHeight = display.getHeight();

熱點內容
解壓到當前文件夾右鍵 發布:2024-04-26 03:57:08 瀏覽:979
html5android教程視頻下載 發布:2024-04-26 03:09:59 瀏覽:867
伺服器的描述是什麼 發布:2024-04-26 03:08:32 瀏覽:394
個人加密 發布:2024-04-26 03:01:23 瀏覽:521
linuxusbgadget 發布:2024-04-26 02:52:54 瀏覽:304
我的世界空島世界伺服器地址 發布:2024-04-26 01:39:08 瀏覽:248
尼爾機械紀元加密 發布:2024-04-26 01:37:11 瀏覽:868
在控制台輸出sql語句 發布:2024-04-26 01:08:12 瀏覽:432
動畫java 發布:2024-04-26 01:02:40 瀏覽:12
得力文件夾5302 發布:2024-04-26 00:21:32 瀏覽:91