样式显示在模拟器中但不在我的设备上

时间:2016-07-23 14:24:47

标签: android xml

我已跟踪style.xml文件夹中的res/values/

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:colorBackground">#000000</item>
</style>

以及我主要活动的以下布局,我使用了style="@style/AppTheme"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    style="@style/AppTheme"                                       // here I've used It.
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kaushal28.wakeupplease.MainActivity">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"
            android:id="@+id/startAlarm" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/stop"
            android:text="Stop"/>
    </LinearLayout>
</RelativeLayout>

我使用Android 4.2.2的设备Jelly Bean和模拟器具有最新的Marsh醇厚。我认为问题仅在于这种差异。我这个项目的最低SDK版本15。

我尝试在targetSDKVersion中将Build.Gradle更改为15。但它无法解决我的问题。

应该展示什么: Image of Emulator.

显示什么: enter image description here

我期待背景中的黑色。

1 个答案:

答案 0 :(得分:0)

尝试使用android:windowBackground代替android:colorBackground

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@android:color/black</item>
</style>

为什么?

在屏幕上,我看到您正在尝试设置窗口背景。因此,最佳属性是android:windowBackground

您的AppTheme未覆盖android:windowBackground ..它只是覆盖android:colorBackground

由于您未覆盖android:windowBackground,Android将使用您的父主题(Theme.AppCompat.Light.DarkActionBar)中定义的颜色。

所以,Android正在使用:

<item name="colorBackgroundFloating">@color/background_floating_material_light</item>

<color name="background_material_light">@color/material_grey_50</color>
<color name="material_grey_50">#fffafafa</color>

它几乎是白色。

设备与工作室

我会说实话..我不完全确定为什么你的设备在模拟器使用时会忽略android:colorBackground ...

可能是API版本或者可能,它只是一种正常的行为,因为您的设备显示应用于Activity的主题并且在整个上下文中。您的设备将始终与Android Studio布局预览工具略有不同

对于任何情况,如果您想发送整个窗口背景,我建议使用android:windowBackground

android:colorBackground不仅可以影响主窗口的背景,还可以影响所有组件的背景,例如对话框,除非您在组件布局中覆盖它。 (这部分是从here

中检索出来的
相关问题