最近在網上看到一篇介紹android window的requestWindowFeature()的使用方法,共用出來大家學習學習
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.dialog_activity);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_dialog_alert);
Android 應用程式表單顯示狀態操作(requestWindowFeature()的應用)
我們在開發程式是經常會需要軟體全螢幕顯示、自訂標題(使用按鈕等控制項)和其他的需求,今天這一講就是如何控制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.預設顯示狀態
2.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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="文本" />
</LinearLayout>
3.FEATURE_INDETERMINATE_PROGRESS詳解
表示一個進程正在運行
實現代碼
1.progress.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar android:id="@+id/progress"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
>
</ProgressBar>
</LinearLayout>
2.Java代碼
this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, R.layout.progress);
setProgressBarIndeterminateVisibility(true);
4.FEATURE_LEFT_ICON詳解
左側顯示表徵圖
實現代碼
this.requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);
5.FEATURE_NO_TITLE詳解
可用於全螢幕顯示
實現代碼
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
雖然上面這樣可以在標題列加入一些控制項,但是仍然不能改變標題列的高度、背景色,要想達到這個目的,只能使用theme(主題)。因此往project裡先添加一個style。改變背景色修改android:windowTitleBackgroundStyle的值,改變標題列高度則修改android:windowTitleSize的值。下面是一個樣本:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#778899</item>
</style>
<style name="activityTitlebar" parent="android:Theme">
<item name="android:windowTitleSize">32dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
接著再修改AndroidManifest.xml檔案,找到要自訂標題列的Activity,添加上android:theme值,比如:
<activity android:name=".MainActivity" android:theme="@style/activityTitlebar">
android:theme值就是上面那個style.xml檔案裡定義的一個style的name值。
按照以上的步驟,修改標題列布局、高度、背景色的功能就實現了。