【问题标题】:Android 5.0(Lollipop): Material Style Not Being AppliedAndroid 5.0(Lollipop):未应用材质样式
【发布时间】:2016-05-18 19:13:59
【问题描述】:

我正在尝试使我的应用程序符合 Google 的材料设计最佳实践,但由于某种原因,我在 v21\styles.xml 中设置的项目从未应用于我的应用程序。

请查看下面的 v21 样式和 Android 清单文件,并告诉我我做错了什么会导致我的自定义 AppTheme 无法显示。

v21\styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- android theme colors -->
        <!-- Main theme colors -->
        <!--   your app branding color for the app bar -->
        <item name="colorPrimary">#F44336</item>
        <!--   darker variant for the status bar and contextual app bars -->
        <item name="colorPrimaryDark">#FFD600</item>
        <!--   theme UI controls like checkboxes and text fields -->
        <item name="colorAccent">#FFD600</item>
    </style>
</resources>

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.foo"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="12"
            android:targetSdkVersion="23" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">   **Theme set here**
            <activity
                android:name=".MainActivity"
                android:configChanges="orientation|keyboardHidden|screenSize"
                android:label="@string/app_name"
                android:theme="@style/FullscreenTheme" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                    <!-- Test:USB otg  -->
                    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                </intent-filter>
                <!-- Test:USB otg  -->
                <meta-data
                    android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
                    android:resource="@xml/device_filter" />
            </activity>
        </application>

    </manifest>

我正在运行最新版本的 Android API 23 的设备上测试此代码,所以我不确定为什么不会触发 API 21+ 的样式。

下面我还发布了我的应用程序的其他平台版本的样式 xml 文件,似乎其中一个正在被应用程序使用。为了完整起见,我已将这些包括在内。

v14\styles.xml

<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
    </style>

</resources>

v11\styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.

    -->
   <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- API 11 theme customizations can go here. -->
    </style>

    <style name="FullscreenTheme" parent="android:Theme.Holo">
        <item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@null</item>
        <item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item>
        <item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item>
    </style>

    <style name="FullscreenActionBarStyle" parent="android:Widget.Holo.ActionBar">
        <item name="android:background">@color/black_overlay</item>
    </style>
</resources>

values\styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--
        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="Theme.AppCompat.Light.NoActionBar">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="FullscreenTheme" parent="android:Theme.NoTitleBar">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@null</item>
        <item name="metaButtonBarStyle">@style/ButtonBar</item>
        <item name="metaButtonBarButtonStyle">@style/ButtonBarButton</item>
    </style>

    <style name="ButtonBar">
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingRight">2dp</item>
        <item name="android:paddingBottom">0dp</item>
        <item name="android:background">@android:drawable/bottom_bar</item>
    </style>

    <style name="ButtonBarButton" />
</resources>

为什么我的应用从不显示我在 v21\styles.xml 中布置的样式??

【问题讨论】:

  • 你怎么知道没有使用 v21 主题? Android 将在 v21 中搜索主题属性,如果未设置样式,则 android 会在默认主题中查找该属性(在 res/values/styles.xml 中)。我只是想获取更多信息来帮助您
  • 我知道没有使用 v21 主题,因为状态栏是黑色而不是我在“colorPrimaryDark”项目中设置的金色。此外,我确信没有使用 v21 主题,因为我尝试将此主题的名称更改为“CustomTheme”并将应用程序的主题设置为“android:theme="@style/CustomTheme">”和状态条仍然是黑色的。

标签: android xml material-design android-theme


【解决方案1】:

我发现了错误。

基本上,在您的清单文件中,您正在为应用程序设置“AppTheme”。但是,您还要为活动设置主题 (FullscreenTheme)。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        android:theme="@style/AppTheme">   **Theme set here**
        <activity
            android:theme="@style/FullscreenTheme" >
        ...               
        </activity>
    </application>
</manifest>

要修复,我看到两个选项:

选项1:

您可以删除:

android:theme="@style/FullscreenTheme"

选项2:

如果您确实需要使用该 FullscreenTheme,请将其添加到 v21/styles.xml。然后,您可以在 v21 中进行不同的设置。

这是你的电话!

【讨论】:

  • 谢谢,它有效。按钮和菜单项现在遵循主题。出于某种原因,当应用程序启动时我的状态栏是金色的,但一旦应用程序初始化,我的状态栏就会再次变黑。知道是什么原因造成的吗?
  • 您应用了哪个修复程序?您是否从清单文件中删除了 FullscreenTheme?
  • 正确:全屏主题已从我的代码中完全删除。
  • 即使你提到的问题发生了?
  • 发生这种情况是因为“Theme.AppCompat.Light.NoActionBar”是您的 AppTheme 的父主题。将其更改为“Theme.AppCompat.Light”或使用任何其他父主题
猜你喜欢
  • 2019-12-22
  • 1970-01-01
  • 1970-01-01
  • 2019-04-23
  • 1970-01-01
  • 2015-01-15
  • 2019-06-02
  • 2016-01-11
相关资源
最近更新 更多