Android:Theme 对旧的 Apis 没有影响

Android:Theme has no effect on older Apis

我一直在尝试使用支持库自定义我的工具栏。虽然设置 android:theme 在 API21+ 上完美运行,但较低的 API 似乎完全忽略它。

这是我的 xml:

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 使工具栏在 API21+ 中有白色图标,在旧设备上图标保持黑色。

更新:我通过向我的 appBarLayout 添加另一个子项 (tabLayout) 进行测试。主题已正确应用。但是即使直接设置支持工具栏也完全拒绝识别主题。

嘿,您必须使用版本号制作样式文件。 为此,转到值并右键单击,然后单击值资源文件 然后将文件命名为样式,然后在可用限定符 select 选项下的对话框中,版本文件然后按图标 >> 然后在其上输入版本 19,此文件适用于低于 [= 的版本20=] 21+

this is the dialog you will get on click of value Resource file

this is the final dialog where you need to enter your version number

可能对你有帮助。

您已在API个基于版本的文件夹中设置主题样式并应用,因此它将基于API个版本应用。

将主题放入 styles.xml。 然后在不同的 API 级别文件夹中复制值文件夹中的 styles.xml [根据您的要求,您必须创建 value-X 文件夹] 请参考下图。

Click here to view image

折腾了几天终于找到问题所在了。我的布局是在调用 super.onCreate() 之前设置的,无论出于何种原因,这使得工具栏无法在未使用本机工具栏的 pre-lolipop 设备中应用正确的主题。

所以我确定:

setContentView(R.layout.main);

在:

之后
super.onCreate(savedinstancestate);