从嵌套样式资源中检索值的语法?

Com*_*are 9 android android-theme

由于DrawerLayout是Android活动的chrome的一部分,抽屉的可能背景颜色似乎是操作栏的背景颜色,因此它们匹配.因此,我想设置ListView抽屉内容与操作栏具有相同的背景颜色,我想在定义的布局XML中这样做ListView.

在这里,我迷失在Android风格系统的迷人小通道的迷宫中......

我知道?android:attr/语法允许您通过引用引用活动使用的主题中定义的值(例如,?android:attr/activatedBackgroundIndicator作为列表项行的背景以使用"激活"状态).

我知道这android:actionBarStyle是主题指向用于设置操作栏本身样式的样式的位置,并且在嵌套(?)样式上,android:background是用于操作栏的背景.

我不知道的是如何制作一个?android:attr/应用于a ListView,从动作栏定义的样式中拉出背景.

这可能吗?如果是这样,语法是什么?

我猜这是不可能的,这就是官方DrawerLayout样本硬编码背景颜色的原因......

谢谢!

Chr*_*nes 8

不幸的是,它不可能通过XML.

您可以在代码中执行以下操作(未经测试,但应该可以工作):

// Need to manually create android.styleable.ActionBar.
// If you need other attributes, add them
int[] android_styleable_ActionBar = { android.R.attr.background };

// Need to get resource id of style pointed to from actionBarStyle
TypedValue outValue = new TypedValue();
getTheme().resolveAttribute(android.R.attr.actionBarStyle, outValue, true);

// Now get action bar style values...
TypedArray abStyle = getTheme().obtainStyledAttributes(outValue.resourceId,
    android_styleable_ActionBar);
// background is the first attr in the array above so it's index is 0.
Drawable bg = abStyle.getDrawable(0);
abStyle.recycle();
Run Code Online (Sandbox Code Playgroud)