使用appcompat-v7将材料主题应用于API 19版及以下版本



我最近下载了Android棒棒糖SDK,我更新了所有依赖项,修复了构建路径,并导入了以下支持库,没有出现问题:

android-support-v7-appcompatandroid-support-v7-网格布局android-support-v7-媒体路由器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")

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

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

尽管这一行在xml中,但我还是得到了以下错误:

错误:找不到与给定名称匹配的资源(位于"bright_foregound_material_light",值为"@color/black")。

不管怎样,我删除了那个特定的行,并成功地在我的手机上安装了应用程序。然而,主题仍然是Holo而不是Material。

有人能告诉我我做错了什么吗?文档非常明确地说,我可以在5.0以下的版本中使用Material主题,并且我遵循了这里列出的关于保持兼容性的说明。

非常感谢您的帮助。

事实证明,我只是将主题应用于我的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;

希望这能帮助到任何有同样问题的人。祝你好运

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

最新更新