android 截取当前应用背景,如何获取当前主题的操作栏背景颜色?

adneal..

6

该ActionBarAPI没有一个方法来检索当前背景Drawable或颜色.

但是,你可以Resources.getIdentifier用来调用View.findViewById,检索ActionBarView,然后调用View.getBackground来检索Drawable.即便如此,这仍然不会给你的颜色.唯一的方法是将其转换Drawable为a Bitmap,然后使用某种颜色分析器来找到主色.

这是一个检索的例子ActionBar Drawable.

final int actionBarId = getResources().getIdentifier("action_bar", "id", "android");

final View actionBar = findViewById(actionBarId);

final Drawable actionBarBackground = actionBar.getBackground();

但似乎最简单的解决方案是创建自己的属性并将其应用于您的主题.

这是一个例子:

自定义属性

初始化属性

@color/your_color_dark

@color/your_color_light

然后在包含您的布局中DrawerLayout,应用如下android:background属性:

android:background="?attr/drawerLayoutBackground"

或者您可以使用a获取它 TypedArray

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

final TypedArray a = obtainStyledAttributes(new int[] {

R.attr.drawerLayoutBackground

});

try {

final int drawerLayoutBackground = a.getColor(0, 0);

} finally {

a.recycle();

}

}

actionBar.getBackground()返回null.难道我做错了什么? (4认同)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值