原創(chuàng)|行業(yè)資訊|編輯:龔雪|2013-11-04 09:48:56.000|閱讀 1070 次
概述:以編程方式處理屏幕布局,鎖定屏幕布局、控制活動生命周期,本文簡明講述以編程方式管理屏幕布局,每個小節(jié)都聚焦于這個主題的不同方面。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
首個顯要的討論點是如何手動處理布局變化。
接下來,我們來考慮應(yīng)該以編程方式做什么來手動處理布局變化。假設(shè)在你的app內(nèi)有一個名為InfoActivity的活動,你將為之以編程方式處理布局變化。在你的AndroidManifest.xml內(nèi),你已經(jīng)有了類似如下InfoActivity的入口:
<activity android:name=".InfoActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> </activity>
作為第一部,你應(yīng)該添加android:configChanges屬性到 activity標簽內(nèi)。
<activity android:name=".InfoActivity" android:configChanges="orientation|keyboardHidden" > <intent-filter> <action android:name="android.intent.action.MAIN" /> </intent-filter> </activity>
額外的線表明無論何時設(shè)備布局改變或者硬件鍵盤被打開, InfoActivity的執(zhí)行都有處理布局變化的邏輯。更多這方面的信息可以在上被找到。
接下來必須執(zhí)行手動處理這個布局變化的邏輯,你需要在InfoActivity中覆蓋onConfigurationChanged()方式。
public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); . . . }
當設(shè)備屏幕布局改變時,AndroidManifest.xml阻礙了新活動自動創(chuàng)建,應(yīng)該為新布局對GUI做任意改變,這需要在被覆蓋的onConfigurationChanged()內(nèi)手動處理。
請注意!即使這些改變在每次屏幕布局發(fā)生變化時阻礙了新活動的創(chuàng)建,它們并不鎖定布局。換言之,從視覺上說你仍然看得到屏幕旋轉(zhuǎn)。鎖定屏幕布局將在下文中提到。
何時手動處理布局變化?
要考慮的下一個問題,是何時處理布局變化?無論屏幕布局何時改變,默認Android框架創(chuàng)建顯示的活動。這是一直都有必要的么?如果不是,什么情況下才有必要?依據(jù)個人經(jīng)驗,上述問題的答案應(yīng)該是:當屏幕布局改變時,它不總是必須回調(diào)顯示的活動并創(chuàng)建再一次為了新布局而創(chuàng)建它。只有當portrait和landscape布局有質(zhì)的不同時才有必要。我們來看兩個簡單的實例。
第一個實例如下顯示列表視圖中一周的日子。在兩種布局中,該視圖都一樣,因此無論屏幕布局幾時變化,沒有必要回調(diào)并創(chuàng)建該活動。
<activity android:name=".Main" android:configChanges="orientation|keyboardHidden" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
public class Main extends ListActivity { private static final String[] MONTHS = new String[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MONTHS)); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // . // Add code if needed // . } }
第二個實例展示兩個圖像,在portrait布局,它們是垂直對齊的;而在landscape布局上,它們是水平對齊的。
顯然,兩種布局有著本質(zhì)的不同,而每當屏幕布局發(fā)生改變,相應(yīng)的活動需要被再次創(chuàng)建。在這種情況下,活動生命周期不能被手動處理。
<activity android:name=".Main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
public class Main extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { setContentView(R.layout.main_p); } else { setContentView(R.layout.main_l); } } }-------------- | main_p.xml | -------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:src="@drawable/image1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_marginLeft="60dip" /> <ImageView android:src="@drawable/image2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_marginLeft="60dip" /> </LinearLayout>-------------- | main_l.xml | -------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:src="@drawable/image1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_marginLeft="20dip" /> <ImageView android:src="@drawable/image2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_marginLeft="40dip"/> </LinearLayout>
鎖定屏幕布局
1,以編程方式
以編程方式鎖定屏幕布局需要專門指示Android應(yīng)該顯示哪個屏幕布局。
如果你不是手動處理屏幕布局,你應(yīng)該在執(zhí)行onCreate()方式中這樣做:
如果你是手動處理屏幕布局,你應(yīng)該指定執(zhí)行onConfigurationChanged()方式所需要的屏幕布局。
2,靜態(tài)化地
或者,你還可以靜態(tài)鎖定屏幕。這是在 AndroidManifest.xml中完成的,在activity標簽中,使用android:screenOrientation屬性。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; setRequestedOrientation(orientation); . // Add code if needed . }
或者
public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); int orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; setRequestedOrientation(orientation); . // Add code if needed . }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)