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认同)

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐