我需要將操作欄的背景更改爲自定義圖像,但每次嘗試使用此代碼時,它都不會改變任何事物。有沒有辦法在Android中通過圖像設置操作欄背景?
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.navbarbg);
BitmapDrawable bd = new BitmapDrawable(getResources(), b);
bar.setBackgroundDrawable(bd);
此外,我試過這段代碼,但它沒有奏效。
Resources res = getResources();
xpp =res.getXml(R.drawable.actionbar_background);
bitmapDrawable = (BitmapDrawable) BitmapDrawable.createFromXml (res, xpp);
actionbar_background.xml的內容
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/navbarbg"
android:tileMode="repeat" />
編輯: 我用這個代碼工作得很好:
Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.action_bar_bg);
BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(actionBarBackground);
感謝要去檢查一下。 –
整個應用程序和整個listView具有相同的背景,使得一切都看起來很糟糕,我試圖在操作欄上使用它,但沒有發生任何事情。也嘗試將風格添加到活動中,活動中的每件事物都具有無法工作的背景。 –
你可能會錯在某個地方。因爲它的工作原理和我在我的項目中一樣。如果你還沒有得到它,用你的代碼編輯你的問題。 – Kanth