Android雜談--layout的橫豎屏處理


From:http://www.cnblogs.com/loulijun/archive/2011/12/22/2296505.html

 

一、layout-land和layout-prot的區別與使用

默認情況下,創建的Android項目里只有一個layout文件夾,盡管這樣也可以橫豎屏切換用,但是某些布局橫屏過后閑的格外的丑,如下圖

橫屏過后就顯示的不全了,有時候看着比較糾結。所以需要在橫屏的使用重新載入新的布局文件

解決辦法是:先把layout目錄刪除了,因為可能跟之后的產生沖突。然后新建兩個文件夾,一個layout-land,另一個是layout-prot。

layout-land:存放橫屏布局文件,如main.xml。布局名字與layout-prot的一樣

layout-prot:存放豎屏布局文件,名字與layout-land的一樣

剩下的事情就可以交由手機處理了,手機會自動處理橫豎屏時布局之間的切換(前提是你的手機支持橫豎屏,並且:設置-顯示-自動旋轉屏幕)

先看看兩個布局文件吧,Activity可以不用管

layout-land/main.xml

復制代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:orientation
="horizontal" >

<LinearLayout
android:orientation="vertical"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:layout_gravity
="center"
android:paddingLeft
="20dip"
android:paddingRight
="20dip"
>
<TextView
android:text="cnblogs-花郎"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center"
android:layout_marginBottom
="20dip"
android:textSize
="24.5sp"
/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center"
android:stretchColumns
="*"
>
<TableRow>
<Button
android:text="吃飯"
/>
<Button
android:text="睡覺"
/>
</TableRow>
<TableRow>
<Button
android:text="旅游"
/>
<Button
android:text="盜墓"
/>
</TableRow>
</TableLayout>
</LinearLayout>

</LinearLayout>
復制代碼

layout-prot/main.xml

復制代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:background
="#3500ffff"
android:padding
="30dip"
android:orientation
="horizontal" >

<LinearLayout
android:orientation="vertical"
android:layout_height
="wrap_content"
android:layout_width
="fill_parent"
android:layout_gravity
="center"
>
<TextView
android:text="cnblogs-花郎"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_gravity
="center"
android:layout_marginBottom
="25dip"
android:textSize
="24.5sp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:text
="吃飯"
/>
<Button
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:text
="睡覺"
/>
<Button
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:text
="旅游"
/>
<Button
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:text
="盜墓"
/>
</LinearLayout>

</LinearLayout>
復制代碼

下面有圖有真相,是橫屏時的布局。

注:切換模擬器的命令式CTRL+F12

這里有一點需要注意的是,橫豎屏切換的生命周期的問題。

在上面的那種情況下,橫豎屏切換的時候,會銷毀Activity后然后再次創建。

在不加入任何設置的時候,它的生命周期是這樣的:

onCreate-onStart-onResume   開始運行時

onPause-onStop-onDestroy-onCreate-onStart-onResume   橫豎屏切換的時候都是這樣

當在AndroidManifest.xml中的activity標簽中加入了:

android:configChanges="orientation|keyboardHidden"

或者android:configChanges="orientation"后

橫豎屏切換就不需要重新載入了,也就是說不用銷毀Activity后再重新創建Activity了。

 

另外一種橫豎屏切換時生命周期的樣子是另外的樣子,是與onConfigureChanges方法有關的,它的生命周期跟我的有些不同呢?

參考:http://hi.baidu.com/%C2%E4%C2%E4%E7%F7%E7%F7%B0%A1/blog/item/dcc19ce5a9c274cc2e2e2178.html

二、如何限定橫屏或者豎屏?

  有些人討厭玩手機的時候橫豎屏來回的切換,有些應用也限定了應用程序只使用橫屏或者只使用豎屏,即使手機設置了“自動切換橫豎屏”。比如“水果忍者”是不能豎屏的(雙人模式除外了)

解決辦法:只需要在AndroidManifest.xml的Activity標簽中加入:android:screenOrientation="landscape"

android:screenOrientation="landscape"表示橫屏

android:screenOrientation="protrait"表示豎屏

這樣,所設定的應用程序就只能是橫屏或者豎屏了

三,橫豎屏切換時關於Activity重新載入的問題(onConfigurationChanged()方法)

例如上面的那個例子,Activity每次橫豎屏切換的時候是重新載入的,但是比如我們在玩游戲的時候切換了一下屏幕,我們不可能要重新玩起,所以需要有一種解決橫豎屏切換的時候保存當前狀態,不用重新載入的方法

解決方案:可以使用onConfigurationChanged方法,該方法可以在用戶切換橫豎屏的時候就不用重新執行onCreate方法了

提醒:這個方法的使用場景比如播放影音的時候轉換了一下屏幕,如果是分別設置了兩個布局的話,那么橫豎屏要對應不同布局,也意味着還是要執行onCreate方法,所以布局最好是一個(不知對不對,求高手指點)

1、需要在AndroidManifest.xml中的對應的activity標簽里加入

android:configChanges="orientation|keyboardHidden"

這條語句的意思是:橫豎屏切換或者實體鍵盤推出合上的時候配置信息的改變

2、需要在AndroidManifest.xml中加入權限

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

3、需要在橫豎屏切換時共用的那個Activity里覆蓋onConfigurationChanged方法,如下

復制代碼
    @Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE )
{
Toast.makeText(getApplicationContext(), "切換為橫屏", Toast.LENGTH_SHORT).show();
}else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
Toast.makeText(getApplicationContext(), "切換為豎屏", Toast.LENGTH_SHORT).show();
}
}
復制代碼

這里需要說的事,代碼中的if語句是判斷當前設備是橫屏還是豎屏,然后有其對應的操作。之前竟然在屏幕切換的時候設置不同的布局,雖然能夠顯示不同的布局,但是這個方法就已經毫無意義了,因為橫豎屏切換到不同的布局我們可以用上面的第一種方法,而這種最好只是對應一個布局吧,然后在里面進行橫豎屏時候的其他操作,防止了重新載入

下面還是看一個例子吧,下面的是一個播放rtsp流的的例子

復制代碼
package com.loulijun.demo07;

import android.app.Activity;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import android.widget.VideoView;

public class Demo07Activity extends Activity {
private VideoView video;
private String rtspUrl = "rtsp://218.205.231.149:554/mobile/1/2CBE124B67C85A59/48f313651199829e.sdp?id=guest&t=1305313158&en=f2ed024c7963e179f65c65689fdd9887&rs=wap";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
video = (VideoView)findViewById(R.id.play);
video.setVideoURI(Uri.parse(rtspUrl));
video.requestFocus();
video.start();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE )
{
Toast.makeText(getApplicationContext(), "切換為橫屏", Toast.LENGTH_SHORT).show();
}else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
Toast.makeText(getApplicationContext(), "切換為豎屏", Toast.LENGTH_SHORT).show();
}
}


}
復制代碼

main.xml

復制代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<VideoView
android:id="@+id/play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>

</LinearLayout>
復制代碼

這里需要說一下VideoView的全屏顯示的問題,橫屏后右邊總是空出一塊黑色區域,即使通過WindowManager的方式也不能解決,索性只能設置布局為居中顯示了,至少好看些

所以只是在清單文件中加入了樣式:android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

這種方式在播放視頻的時候不能全屏,希望大牛們可以提出自己的解決方案
AndroidManifest.xml

復制代碼
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.loulijun.demo07"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:name=".Demo07Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<uses-permission android:name="andorid.permission.CHANGE_CONFIGURATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
復制代碼

看看運行效果吧

      

文章精選:

http://www.blogjava.net/zh-weir/archive/2010/01/24/310617.html

http://www.cnblogs.com/hibraincol/archive/2010/09/18/1829862.html

http://hi.baidu.com/gaogaf/blog/item/6ef7184c3b20bff6d72afc2a.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM