我试图使我的导航抽屉的背景总是与动作栏的背景色相匹配。
这样,每次,如果主题发生变化,两个背景都会自动改变。
我调查了R.attr,但什么也没发现。
发布于 2014-04-04 20:56:43
ActionBar API没有检索当前背景Drawable或颜色的方法。
但是,您可以使用Resources.getIdentifier调用View.findViewById,检索ActionBarView,然后调用View.getBackground检索Drawable。即使如此,这仍然不能给你颜色。唯一做到这一点的方法是将Drawable转换为Bitmap,然后使用某种颜色分析器来查找主导颜色。
下面是检索ActionBar Drawable的示例。
final int actionBarId = getResources().getIdentifier("action_bar", "id", "android");
final View actionBar = findViewById(actionBarId);
final Drawable actionBarBackground = actionBar.getBackground();但似乎最简单的解决方案是创建自己的属性并将其应用到主题中。
这里有一个例子:
自定义属性
<attr name="drawerLayoutBackground" format="reference|color" />初始化属性
<style name="Your.Theme.Dark" parent="@android:style/Theme.Holo">
<item name="drawerLayoutBackground">@color/your_color_dark</item>
</style>
<style name="Your.Theme.Light" parent="@android:style/Theme.Holo.Light">
<item name="drawerLayoutBackground">@color/your_color_light</item>
</style>然后,在包含DrawerLayout的布局中,应用android:background属性,如下所示:
android:background="?attr/drawerLayoutBackground"或者您可以使用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();
}
}https://stackoverflow.com/questions/22871492
复制相似问题