解決android自定義標題欄充滿的問題 博客分類: Android AndroidEclipseXMLvimGit

        一個接着一個的activity,寫啊寫,調啊調,後來,終於發覺,activity的標題欄好難看,好單調啊。咱們爲了吸引用戶的眼球,得搞點個性化的東西。         自定義標題欄的方法,網上一搜一大堆,我也稍微提一下,oncreate中加上如下代碼就行:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(view);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

         這個名爲title的layout是這樣子的,很簡單,就是一個textview,然後有個背景色:

<?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"
    android:background="#66cccccc"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="hello"
    />
</LinearLayout>

         好,運行看效果。看到了吧,發現問題了沒,標題欄的背景色沒有填充滿是吧,這可真是杯具喲。padding、margin什麼的都用上也不管用,怎麼辦呢。     看源碼!         window初始化,加載標題的地方,咱也不知道在哪裏,不過咱能以layout作爲切入點。打開源碼裏面的layout文件夾,找跟標題欄相關的xml文件。裏面有screen_title.xml和screen_custom_title.xml,這就是咱們要找的目標了。         既然是自定義標題,那我們就看screen_custom_title.xml,裏面有一個title_container和一個content,組合成了標題欄,我們自定義標題所給出的view,都被content作爲子view了,影響不了那個title_container和content,所以,任你怎麼弄,它該留白的還是留白,你沒招。     看title_container有個style是這樣的:style="?android:attr/windowTitleBackgroundStyle"     content的foreground是這樣的android:foreground="?android:attr/windowContentOverlay"     好,從這裏我們就可以入手改了。

去values下面的themes.xml找到windowTitleBackgroundStyle這一項,這個應該在註釋<!-- Window attributes -->的下面。

<item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>

然後去styles.xml下找到WindowTitleBackground項,

<style name="WindowTitleBackground">
        <item name="android:background">@android:drawable/title_bar</item>
</style>

發現是一個drawable,xml的,裏面定義了背景圖片。ok,我們知道了,這個是定義titlebar的背景色。

    然後,去values下面的themes.xml找到windowContentOverlay,也是屬於window attributes。

<item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>

    發現也是個drawable,ok,我們也知道了,這個是定義contentoverlay的背景的。     其實,通過研究我發現,不能填充滿的原因是title_container的背景的原因,我們覆蓋一下就行了。

首先,寫個themes文件

<resources>
    <style name="XTheme" parent="android:Theme">    
    	
        <!-- Window attributes -->
        <item name="android:windowTitleStyle">@style/XWindowTitle</item>   
        <item name="android:windowTitleBackgroundStyle">@style/StatusBarBackground</item>     
        <item name="android:windowContentOverlay">@null</item>
	</style>	
</resources>

然後寫styles文件

<resources>    
    <style name="StatusBarBackground">
        <item name="android:background">@drawable/shape</item>
    </style>
            
    <style name="XWindowTitle" parent="android:WindowTitle">
        <item name="android:shadowColor">#BB000000</item>
        <item name="android:shadowRadius">0</item>
    </style>
</resources>

注意這個XWindowTitle要繼承WindowTitle。

   最後,在manifext中給自定義的activity申明主題。

       <activity android:name=".Entry"
                  android:label="@string/app_name"
                  android:theme="@style/XTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

好,我們來看看效果吧:

      so cool, isn't it?

      當然,你也可以換成別的顏色或者是更炫的圖片做背景。

詳細實例代碼見附件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章