【问题标题】:Android Theming: How do I reference a drawable out of the theme selected in AndroidManifest.xml?Android 主题:如何从 AndroidManifest.xml 中选择的主题中引用可绘制对象?
【发布时间】:2014-01-06 01:02:31
【问题描述】:

我正在将我的应用程序从自定义标题栏切换到 ActionBar 支持库,因为我喜欢 ActionBar 提供的功能,而我之前避免使用它,因为它只是 3.0+。我的 ActionBar 工作正常,这很好,但我的应用程序在屏幕底部还有一个较低的栏,我希望保持与顶部的 ActionBar 相同的外观。由于看起来 ActionBar 使用图像背景(九个补丁 PNG)而不是像我以前那样定义颜色,我需要从我的布局 xml 访问操作栏底部可绘制对象的可绘制对象,并将其用作底部栏的背景。

我查看了支持 v7 库,发现像 @drawable/abc_cab_background_bottom_holo_dark 这样的可绘制对象,并且手动输入它非常适合深色主题。但是,我想添加一些东西,可以自动为 AndroidManifest 中指定的主题拉取正确的可绘制对象,无论是浅色主题还是深色主题。

下面是一些有问题的 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">

在 AndroidManifest.xml 中:

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

我发现的所有内容都在讨论如何使用您自己的主题编辑 ActionBar,但我想做相反的事情,使用基于股票 appcompat 主题的股票 ActionBar 可绘制来编辑我自己的栏。具体来说,我应该为 android:background= 设置什么来引用当前主题的版本:

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

在 v7/appcompat/res/values/themes_base.xml 中定义?

【问题讨论】:

    标签: android xml android-layout android-actionbar themes


    【解决方案1】:

    嘿,来自我的 ActionBar 示例应用程序。

    我正在像这样以编程方式进行操作:

    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" />
    

    在 XML 部分将你的 drawable 设置为图像源,然后在程序中使用 XML 作为 BitampDrawable

    在 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);
            }
    

    编辑: R.drawable.bg_stripedR.drawable.bg_striped_split

    这两个都是 XML 文件,如上所述,bg_striped 用于更改您的 actionBar 背景,bg_striped_split 用于更改您的拆分菜单背景..

    【讨论】:

    • R.drawable.bg_striped_split 在哪里定义?
    • 刚刚编辑..!!请告诉我它是否有效..:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多