安卓開發-自訂標題列

來源:互聯網
上載者:User

標籤:android   style   http   io   color   ar   sp   java   for   

一個接著一個的activity,寫啊寫,調啊調,後來,終於發覺,activity的標題列好難看,好單調啊。咱們為了吸引使用者的眼球,得搞點個人化的東西。

  自訂標題列的方法,網上一搜一大堆,我也稍微提一下,oncreate中加上如下代碼就行:

  Java代碼

  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

  setContentView(view);

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

  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

  setContentView(view);

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

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

  Xml代碼

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?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這一項,這個應該在注釋的下面。

  Xml代碼

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>  

 

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

  Xml代碼

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><style name="WindowTitleBackground">  
        <item name="android:background">@android:drawable/title_bar</item>  
</style>

 

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

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

  Xml代碼

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>  

 

 

  發現也是個drawable,ok,我們也知道了,這個是定義contentoverlay的背景的。

  其實,通過研究我發現,不能填充滿的原因是title_container的背景的原因,我們覆蓋一下就行了。

  首先,寫個themes檔案

  Xml代碼

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><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檔案

  Xml代碼

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><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申明主題。

  Xml代碼

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><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?

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

安卓開發-自訂標題列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.