Android - Custom ActionBar

摘要:Android - Custom ActionBar

客製化ActionBar ,參考以下兩個連結

http://blog.csdn.net/pathuang68/article/details/6646792

http://sean1223.blogspot.tw/2013_07_01_archive.html

 

首先,建立一個Layout  ,  custom_actionbar.xml 

在onCreate 撰寫


        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_actionbar);

 

以上順序必須要一致
 
再來執行的話,應該會遇到連結二所說的問題
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
 
就要調整style
style,加入以下 - (範例)

    <style name= "CustomizedWindowTitleBackgroundColor" >
         <item name= "android:background" >@color/pink</item>
    </style>

<style name="TitleBarTheme" parent="android:Theme">
   <item name= "android:windowTitleBackgroundStyle" >@style/CustomizedWindowTitleBackgroundColor</item>
   <item name="android:windowBackground">@color/white</item>
   <item name="android:windowTitleSize" >38dip</item>
</style>

 

再到 AndroidManifest 找到需要改的activity 設定如下

        <activity
            android:name="xxxxxxxxx.MainActivity"
            android:screenOrientation="portrait" 
            android:theme="@style/TitleBarTheme">
        </activity>