简体   繁体   English

Android主题:如何从AndroidManifest.xml中选择的主题中引用可绘制对象?

[英]Android Theming: How do I reference a drawable out of the theme selected in AndroidManifest.xml?

I'm switching my app over from a custom title bar to ActionBar support library as I like the features ActionBar provides and I previously avoided it due to being 3.0+ only. 我将我的应用程序从自定义标题栏切换到ActionBar支持库,因为我喜欢ActionBar提供的功能,并且由于仅3.0+,我以前避免了它。 I have the ActionBar working properly which is nice, but my app also has a lower bar at the bottom of the screen that I want to keep the same look as the ActionBar at the top. 我的ActionBar正常工作,这很好,但是我的应用程序在屏幕底部也有一个较低的栏,我希望与顶部的ActionBar保持相同的外观。 Since it appears ActionBar uses image backgrounds (nine patch PNG) rather than defining colors like I previously was doing I need to access the drawable for the action bar bottom drawable from my layout xml and use that as the background for my bottom bar. 由于ActionBar似乎使用图像背景(九个补丁PNG),而不是像我以前那样定义颜色,因此我需要从布局xml访问动作条底部可绘制对象的drawable,并将其用作底部条形的背景。

I looked through the support v7 library and found drawables like @drawable/abc_cab_background_bottom_holo_dark and entering that manually works great with the dark theme. 我浏览了支持v7库,发现了诸如@ drawable / abc_cab_background_bottom_holo_dark之类的可绘制对象,并手动输入该对象可很好地适用于深色主题。 However, I would like to put in something that automatically pulls the correct drawable for the theme specified in AndroidManifest, whether it be light or dark theme. 但是,我想添加一些东西,它可以自动为AndroidManifest中指定的主题提取正确的可绘制对象,无论它是浅色还是深色主题。

Here's the bit of XML in question: 这是有问题的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:background="@drawable/abc_ab_bottom_solid_light_holo">

and in AndroidManifest.xml: 并在AndroidManifest.xml中:

<application android:icon="@drawable/icon"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:hardwareAccelerated="true"
        android:theme="@style/Theme.AppCompat.Light" >

Everything I've found has been discussing how to edit the ActionBar with your own theme but I want to do the opposite, edit my own bar with the stock ActionBar drawable based on stock appcompat themes. 我发现的所有内容都在讨论如何使用您自己的主题编辑ActionBar,但我想做相反的事情,使用基于股票appcompat主题的股票ActionBar可绘制对象来编辑自己的酒吧。 Specifically, what do I put for android:background= to reference the current theme's version of: 具体来说,我应该为android:background =放置什么以引用当前主题的版本:

<item name="actionModeSplitBackground">@drawable/abc_cab_background_bottom_holo_dark</item>

as defined in v7/appcompat/res/values/themes_base.xml? 如v7 / appcompat / res / values / themes_base.xml中所定义?

Hey from my sample app for ActionBar. 嘿,从我的ActionBar示例应用程序开始。

I'm doing it Programmatically Like this: 我正在以编程方式这样做:

XML Part: XML部分:

<?xml version="1.0" encoding="utf-8"?>

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/bg_striped_img"
    android:tileMode="repeat"
    android:dither="true" />

In XML part set your drawable as image source and then use XML in program as BitampDrawable 在XML部分中,将您的drawable设置为图像源,然后在程序中将XML用作BitampDrawable

on OnCreate : 在OnCreate上:

//striped layout
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped);
            bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            getSupportActionBar().setBackgroundDrawable(bg);

            BitmapDrawable bgSplit = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped_split);
            bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
            getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
        }

EDIT: R.drawable.bg_striped and R.drawable.bg_striped_split 编辑: R.drawable.bg_stripedR.drawable.bg_striped_split

These Both are you XML file as stated above and bg_striped is to change your actionBar Background and bg_striped_split is to change your Split menu background.. 这两个都是如上所述的XML文件,bg_striped用于更改actionBar背景,bg_striped_split用于更改Split菜单背景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要在AndroidManifest.xml中为每个活动指定主题吗 - Do I need to specify the theme for each activity in the AndroidManifest.xml 如何在AndroidManifest.xml中应用主题? - How to apply theme in AndroidManifest.xml ? 如何在AndroidManifest.xml中参数化Activity - How do I parameterize an Activity from within AndroidManifest.xml 如何在Android设备中安装的应用程序的AndroidManifest.xml文件中获取权限? - How do I get permissions only in AndroidManifest.xml file for installed apps in android device? Java / Android:如何访问AndroidManifest.xml中声明的ContentProvider? - Java/Android: how do I access the ContentProvider that is declared in AndroidManifest.xml? 如何在Android Studio中验证AndroidManifest.xml? - How is AndroidManifest.xml validated in android studio? 如何在Android上以编程方式编译AndroidManifest.xml? - How to compile AndroidManifest.xml programmatically on Android? 如何获取 android 应用程序的 AndroidManifest.xml - How to get AndroidManifest.xml of an android app 如果我动态注册了BroadcastReceiver,是否需要在AndroidManifest.xml中填写“ receiver”信息? - Do I need to fill out the 'receiver' info in AndroidManifest.xml if I register a BroadcastReceiver dynamically? android:screenOrientation 在 AndroidManifest.xml 中到底做了什么? - What exactly does android:screenOrientation do in the AndroidManifest.xml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM