Dav*_*Liu 8 android background-color android-theme android-actionbar
我试图让导航抽屉的背景始终与动作栏的背景颜色相匹配.
所以,每次,如果主题改变,两个背景都会自动改变.
我看着R.attr,但没有找到任何东西.
该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();
Run Code Online (Sandbox Code Playgroud)
但似乎最简单的解决方案是创建自己的属性并将其应用于您的主题.
这是一个例子:
自定义属性
<attr name="drawerLayoutBackground" format="reference|color" />
Run Code Online (Sandbox Code Playgroud)
初始化属性
<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>
Run Code Online (Sandbox Code Playgroud)
然后在包含您的布局中DrawerLayout,应用如下android:background属性:
android:background="?attr/drawerLayoutBackground"
Run Code Online (Sandbox Code Playgroud)
或者您可以使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5561 次 |
| 最近记录: |