Android雜談–layout的橫豎屏處理

來源:互聯網
上載者:User

我的部落格搬家了,以前是http://hualang.iteye.com/,現在挪到這裡了

  橫豎屏處理是開發應用是比較基礎的一個要點,幾乎都會用到。網上有一大堆的橫豎屏切換的文章,但是翻了n頁以後發現竟然清一色的是轉載,所以不想浪費時間到這個上面,還是自己根據自己的需求與體會總結一下吧,也方便以後查閱

一、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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.