有没有办法在android中按图像设置动作栏背景? [英] Is there a way to set actionbar backgound by image in android?

查看:19
本文介绍了有没有办法在android中按图像设置动作栏背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将操作栏的背景更改为自定义图像,但每次我尝试使用此代码时都不会改变任何内容.

I need to change background of the action bar to a customized image, but every time I try to use this code it does not change a thing.

Bitmap b =  BitmapFactory.decodeResource(getResources(), R.drawable.navbarbg);
BitmapDrawable bd = new BitmapDrawable(getResources(), b);
bar.setBackgroundDrawable(bd);

我也试过这段代码,但没有用.

Also I tried this code but it didn't work.

Resources res = getResources();
xpp =res.getXml(R.drawable.actionbar_background);
bitmapDrawable = (BitmapDrawable) BitmapDrawable.createFromXml (res, xpp);

actionbar_background.xml的内容是

The contents of actionbar_background.xml are

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/navbarbg"
android:tileMode="repeat" />

我使用了这段代码,效果很好:

Edited: I used this code which worked fine:

Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.action_bar_bg);
BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(actionBarBackground);

推荐答案

我使用了这段代码,效果很好:

I used this code which worked fine:

Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.action_bar_bg);
BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(actionBarBackground);

这篇关于有没有办法在android中按图像设置动作栏背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆