public class ActivityEx extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_test); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_actionbar); } }XML佈局文件, 略.
<style name="ActionBar.Style.WindowTitle"> <item name="android:textColor">#fff</item> <item name="android:textSize">14sp</item> <item name="android:textStyle">bold</item> </style> <style name="ActionBar.Style"> <item name="android:singleLine">true</item> <item name="android:textAppearance">@style/ActionBar.Style.WindowTitle</item> <item name="android:shadowColor">#BB000000</item> <item name="android:shadowRadius">2.75</item> </style> <style name="ActionBar.Background"> <item name="android:background">@null</item> </style> <style name="ActionBar" parent="android:Theme"> <item name="android:windowTitleStyle">@style/ActionBar.Style</item> <item name="android:windowTitleSize">56dp</item> <item name="android:windowTitleBackgroundStyle">@style/ActionBar.Background</item> </style>
P.S. 可參考 \android-sdk\platforms\android-[$level]\data\res\values\themes.xml html
3. 編輯AndroidManifest.xml, 啓用自定義樣式 java
<activity android:name="net.oschina.my.ActionBarTest" android:theme="@style/ActionBar" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
若是按照默認的工程設置, 並只編寫Java代碼, 那麼App運行時就會報錯.
所以, 爲了保險起見, 強制設置Activity的theme, 而且該theme是繼承於android:Theme, 不然也將會報錯. 錯誤日誌以下:
android
You cannot combine custom titles with other title features根據報錯日誌, 咱們能夠看到拋異常的類是: android-[$level]\com\android\internal\policy\impl\PhoneWindow.java , 從中能夠找到 requestFeature 函數, 以下:
@Override public boolean requestFeature(int featureId) { if (mContentParent != null) { throw new AndroidRuntimeException("requestFeature() must be called before adding content"); } final int features = getFeatures(); if ((features != DEFAULT_FEATURES) && (featureId == FEATURE_CUSTOM_TITLE)) { /* Another feature is enabled and the user is trying to enable the custom title feature */ throw new AndroidRuntimeException("You cannot combine custom titles with other title features"); } if (((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) && (featureId != FEATURE_CUSTOM_TITLE) && (featureId != FEATURE_ACTION_MODE_OVERLAY)) { /* Custom title feature is enabled and the user is trying to enable another feature */ throw new AndroidRuntimeException("You cannot combine custom titles with other title features"); } if ((features & (1 << FEATURE_NO_TITLE)) != 0 && featureId == FEATURE_ACTION_BAR) { return false; // Ignore. No title dominates. } if ((features & (1 << FEATURE_ACTION_BAR)) != 0 && featureId == FEATURE_NO_TITLE) { // Remove the action bar feature if we have no title. No title dominates. removeFeature(FEATURE_ACTION_BAR); } return super.requestFeature(featureId); }
從上面的代碼基本能夠知道, 爲何須要強制設置theme, 而且該theme仍是繼承於android:Theme. app