简体   繁体   中英

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. 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. 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.

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. 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.

Here's the bit of XML in question:

<?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:

<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. Specifically, what do I put for android:background= to reference the current theme's version of:

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

as defined in v7/appcompat/res/values/themes_base.xml?

Hey from my sample app for ActionBar.

I'm doing it Programmatically Like this:

XML Part:

<?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

on 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

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM