通過該執行個體的擴充可以在自訂標題中做出菜單導航等實用的功能,
1、給自訂標題提供一個介面
2、將自訂標題應用給Activity視窗
3、把android系統為Activity設定的預設主題改為自己的主題
============================下面查看實現該例子的具體代碼================================
step2、編寫自訂標題的布局檔案 /res/layout/custom_title.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" android:background="@drawable/rectangle"> <!-- 指定背景,該背景自己畫的 --><Button android:id="@+id/infoAtMeTextView"android:textColor="#FF0000" android:layout_width="wrap_content"android:layout_height="match_parent" android:layout_weight="1"android:gravity="center" android:text="\@我" /><Button android:id="@+id/infoCommentTextView"android:textColor="#FF0000" android:layout_width="wrap_content"android:layout_height="match_parent" android:layout_weight="1"android:gravity="center" android:text="評論" /><Button android:id="@+id/infoPrivateMsgTextView"android:textColor="#FF0000" android:layout_width="wrap_content"android:layout_height="match_parent" android:layout_weight="1"android:gravity="center" android:text="私信" /></LinearLayout>
step3、上面布局檔案中使用的背景是一個drawable檔案,該drawable檔案繪製了一個長方形。該檔案是/drawable/rectangle.xml
<?xml version="1.0" encoding="utf-8"?><!-- 下面定義了一個長方形 --><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><gradient android:angle="270" android:endColor="#1DC9CD"android:startColor="#A2E0FB" /><padding android:left="2dp" android:top="2dp" android:right="2dp"android:bottom="2dp" /></shape>
step4、將自訂標題設定到Activity中,CustomTitleActivity.java
package cn.oyp.title;import android.app.Activity;import android.os.Bundle;import android.view.Window;public class CustomTitleActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //指定使用自訂標題 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); //設定視窗的自訂標題布局檔案 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); }}
然後運行該應用,發現使用者佈建後的自訂layout沒有辦法填充整個標題列。
通過查看Android原始碼得知Android系統為Activity的title預設設定了一個布局檔案,該布局檔案是core/res/res/layout/screen_title.xml
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2006 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.--><!--This is an optimized layout for a screen, with the minimum set of featuresenabled.--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:fitsSystemWindows="true"> <FrameLayout android:layout_width="match_parent" android:layout_height="?android:attr/windowTitleSize" style="?android:attr/windowTitleBackgroundStyle"> <TextView android:id="@android:id/title" style="?android:attr/windowTitleStyle" android:background="@null" android:fadingEdge="horizontal" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> <FrameLayout android:id="@android:id/content" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:foregroundGravity="fill_horizontal|top" android:foreground="?android:attr/windowContentOverlay" /></LinearLayout>
讀上一段代碼可以發現該布局檔案由兩個幀布局構成,而其中的幾個屬性
標題高度
標題背景樣式
?android:attr/windowContentOverlay 標題前景色彩
而這幾個屬性的值都是在core/res/res/values/themes.xml檔案中被賦值了
<style name="Theme"><item name="android:windowContentOverlay">@android:drawable/title_bar_shadow</item><item name="android:windowTitleSize">25dp</item><item name="android:windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item></style>
而上面的@android:style/WindowTitleBackground樣式在core/res/res/values/styles.xml檔案中被定義
<style name="WindowTitleBackground"><item name="android:background">@android:drawable/title_bar</item></style>
step5、自訂樣式 /res/values/style.xml
<?xml version="1.0" encoding="utf-8"?><resources><!-- 該樣式繼承系統的預設樣式 --><style name="customTheme" parent="android:Theme"><!-- 設定標題前景色彩為透明 --><item name="android:windowContentOverlay">@drawable/nocolor</item><!-- 設定標題高度為44dp --><item name="android:windowTitleSize">44dp</item><!-- 設定標題背景色 --><item name="android:windowTitleBackgroundStyle">@style/customBg</item></style><!-- 定義一個背景樣式 --><style name="customBg"><item name="android:background">@drawable/rectangle</item></style></resources>
上面的@drawable/nocolor定義在/res/values/strings.xml檔案中
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">該應用的目的是自訂視窗標題!</string> <string name="app_name">自訂視窗標題</string> <!-- 定義一個透明色 --><drawable name="nocolor">#00000000</drawable></resources>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.oyp.title" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".CustomTitleActivity" android:theme="@style/customTheme"><!-- 使用自訂佈景主題 --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
step7:查看該自訂視窗的效果