【问题标题】:Using appcompat-v7 to apply Material Theme to API Version 19 and Below使用 appcompat-v7 将 Material Theme 应用到 API 版本 19 及以下
【发布时间】:2014-10-18 18:30:17
【问题描述】:

我最近下载了 Android Lollipop SDK,并更新了所有依赖项、修复了构建路径并顺利导入了以下支持库:

android-support-v7-appcompat android-support-v7-gridlayout android-support-v7-mediarouter android-support-v7-调色板

我的应用使用蓝牙低功耗,所以我的minSDK版本是18,targetSDK是最新的; 21.

当我尝试使用以下 xml 应用材料主题(使用 appcompat-v7)时,我的问题出现了:

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="colorPrimary">@color/material_blue_500</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorAccent">@color/material_green_A200</item>
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    <!-- <item name="android:actionBarStyle">@style/AppBaseActionBar</item> -->
</style>

<!-- the theme applied to the application or activity -->

当我尝试运行它时,出现以下错误:

错误:未找到与给定名称匹配的资源(在“colorAccent”处,值为“@color/material_green_A200”)

经过一番谷歌搜索后,我发现了 3 天前更新的这个 github 存储库:gist.github.com/eyecatchup/b5e564cfe11619cc3999

在那里,大多数材料设计颜色都已定义,所以我只是将其复制到我的主题的 color.xml 中。

尽管该行在 xml 中,但我仍然收到以下错误:

错误:未找到与给定名称匹配的资源(位于“bright_foreground_material_light”,值为“@color/black”)。

无论如何,我删除了该特定行并成功在手机上安装了该应用程序。但是,主题仍然是 Holo 而不是 Material。

谁能告诉我我做错了什么? docs 非常清楚地表明我可以在低于 5.0 的版本中使用 Material 主题,并且我按照 here 列出的关于保持兼容性的说明进行操作。

非常感谢您的帮助。

【问题讨论】:

  • 按名称“material_green_A200”在资源中添加颜色...

标签: android android-5.0-lollipop android-support-library


【解决方案1】:

原来我只是将主题应用到我的 values 文件夹中的自定义主题,而 values-v11 和 values-v14 主题 xml 保持不变。

解决了这个问题,我通过更新我的活动以将 ActionBarActivity 和我的片段管理器扩展到 getSupportFragmentManager() 管理器而不是 getFragmentManager() 来运行材料主题。

如果您碰巧使用了导航抽屉,在 getActionBar 中,当为您的 ListView 设置适配器时,您需要将 ActionBar 转换为 SupportActionBar,如下所示:

private ActionBar getSupportActionBar() {
    return ((ActionBarActivity)getActivity()).getSupportActionBar();
}

并按照您通常的方式设置适配器:

        mDrawerListView.setAdapter(
            new ArrayAdapter<String>(getSupportActionBar()
            .getThemedContext(),
            R.layout.fragment_navigationdrawer_textview,
            android.R.id.text1, drawerOptions));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
    return mDrawerListView;

希望能帮助遇到与我相同问题的任何人。祝你好运。

【讨论】:

    【解决方案2】:

    您必须经常创建新项目,然后 ID 会自动生成 appcompat_v7 库,然后右键单击您的旧错误项目单击属性,然后单击 android 并在您的项目中添加 appcompat_v7 库。

    【讨论】:

    • 我已经将 appcompat_v7 库添加到我的项目中。我的项目目前没有错误并且运行良好,唯一的问题是尽管使用了 appcompat_v7 材质主题,但主题仍然是 Holo。
    猜你喜欢
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    相关资源
    最近更新 更多