原創|行業資訊|編輯:龔雪|2013-11-04 09:48:56.000|閱讀 1077 次
概述:以編程方式處理屏幕布局,鎖定屏幕布局、控制活動生命周期,本文簡明講述以編程方式管理屏幕布局,每個小節都聚焦于這個主題的不同方面。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
首個顯要的討論點是如何手動處理布局變化。
接下來,我們來考慮應該以編程方式做什么來手動處理布局變化。假設在你的app內有一個名為InfoActivity的活動,你將為之以編程方式處理布局變化。在你的AndroidManifest.xml內,你已經有了類似如下InfoActivity的入口:
<activity android:name=".InfoActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
作為第一部,你應該添加android:configChanges屬性到 activity標簽內。
<activity android:name=".InfoActivity"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
額外的線表明無論何時設備布局改變或者硬件鍵盤被打開, InfoActivity的執行都有處理布局變化的邏輯。更多這方面的信息可以在上被找到。
接下來必須執行手動處理這個布局變化的邏輯,你需要在InfoActivity中覆蓋onConfigurationChanged()方式。
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
.
.
.
}
當設備屏幕布局改變時,AndroidManifest.xml阻礙了新活動自動創建,應該為新布局對GUI做任意改變,這需要在被覆蓋的onConfigurationChanged()內手動處理。
請注意!即使這些改變在每次屏幕布局發生變化時阻礙了新活動的創建,它們并不鎖定布局。換言之,從視覺上說你仍然看得到屏幕旋轉。鎖定屏幕布局將在下文中提到。
何時手動處理布局變化?
要考慮的下一個問題,是何時處理布局變化?無論屏幕布局何時改變,默認Android框架創建顯示的活動。這是一直都有必要的么?如果不是,什么情況下才有必要?依據個人經驗,上述問題的答案應該是:當屏幕布局改變時,它不總是必須回調顯示的活動并創建再一次為了新布局而創建它。只有當portrait和landscape布局有質的不同時才有必要。我們來看兩個簡單的實例。
第一個實例如下顯示列表視圖中一周的日子。在兩種布局中,該視圖都一樣,因此無論屏幕布局幾時變化,沒有必要回調并創建該活動。
<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布局上,它們是水平對齊的。
顯然,兩種布局有著本質的不同,而每當屏幕布局發生改變,相應的活動需要被再次創建。在這種情況下,活動生命周期不能被手動處理。
<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應該顯示哪個屏幕布局。
如果你不是手動處理屏幕布局,你應該在執行onCreate()方式中這樣做:
如果你是手動處理屏幕布局,你應該指定執行onConfigurationChanged()方式所需要的屏幕布局。
2,靜態化地
或者,你還可以靜態鎖定屏幕。這是在 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
.
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:慧都控件網