我們在開發程式是經常會需要軟體全螢幕顯示、自訂標題(使用按鈕等控制項)和其他的需求,今天這一講就是如何控制Android應用程式的表單顯示.
首先介紹一個重要方法那就是requestWindowFeature(featrueId),它的功能是啟用表單的擴充特性。參數是Window類中定義的常量。
一、枚舉常量
1.DEFAULT_FEATURES:系統預設狀態,一般不需要指定
2.FEATURE_CONTEXT_MENU:啟用ContextMenu,預設該項已啟用,一般無需指定
3.FEATURE_CUSTOM_TITLE:自訂標題。當需要自訂標題時必須指定。如:標題是一個按鈕時
4.FEATURE_INDETERMINATE_PROGRESS:不確定的進度
5.FEATURE_LEFT_ICON:標題列左側的表徵圖
6.FEATURE_NO_TITLE:吳標題
7.FEATURE_OPTIONS_PANEL:啟用“選項面板”功能,預設已啟用。
8.FEATURE_PROGRESS:進度列指示器功能
9.FEATURE_RIGHT_ICON:標題列右側的表徵圖
二、詳解
預設顯示狀態
1.FEATURE_CUSTOM_TITLE詳解
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
這是因為沒設定Featrue
在上面代碼後加:getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
自訂標題完成,它是一個xml檔案布局
title.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="wrap_content" 5 android:layout_height="wrap_content" > 6 7 <ImageView android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:src="@drawable/icon"/>10 <TextView android:id="@+id/text" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_alignParentLeft="true" 14 android:text="文本" /> 15 16 </LinearLayout>
3.FEATURE_INDETERMINATE_PROGRESS詳解
表示一個進程正在運行
實現代碼
1.progress.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="wrap_content" 5 android:layout_height="wrap_content"> 6 <ProgressBar android:id="@+id/progress" 7 8 android:layout_width="wrap_content" 9 10 android:layout_height="wrap_content" 11 12 android:layout_gravity="center_vertical"13 style="?android:attr/progressBarStyleSmallTitle">14 15 </ProgressBar>16 </LinearLayout>
2.Java代碼
1 this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);2 setContentView(R.layout.main);3 4 5 getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);6 setProgressBarIndeterminateVisibility(true);
3.FEATURE_LEFT_ICON詳解
左側顯示表徵圖
實現代碼
1 this.requestWindowFeature(Window.FEATURE_LEFT_ICON);2 setContentView(R.layout.main);3 4 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);
4.FEATURE_NO_TITLE詳解
可用於全螢幕顯示
實現代碼
1 this.requestWindowFeature(Window.FEATURE_NO_TITLE);2 setContentView(R.layout.main);3 4 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
圖6全螢幕顯示
轉:http://www.cnblogs.com/salam/archive/2010/11/30/1892143.html